# Encoding issue for special characters on Windows

**URL:** <https://www.ruby-forum.com/t/encoding-issue-for-special-characters-on-windows/157307>\
**Category:** Ruby\
**Created:** [January 9, 2009, 10:15am UTC](https://www.ruby-forum.com/t/encoding-issue-for-special-characters-on-windows/157307 "2009-01-09T10:15:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 9, 2009, 10:15am UTC](https://www.ruby-forum.com/t/encoding-issue-for-special-characters-on-windows/157307/1 "2009-01-09T10:15:30Z")

</div>

Hi,

I am facing an issue with special characters handling inside a Ruby  
script running on Windows and am sure some of you could help me on  
this.

This script copies files such as “\<English\_name\>.txt” to  
“\<Other\_language\_name\>.txt”. But once translated, the new filename may  
have special characters. ‘ä’ for instance.

Running  
puts  
'ä’in a Ruby script gives  
'õ’as an output, whereas the same code in irb gives  
‘ä’  
There must be an encoding issue at some point in my script but I  
didn’t manage to fix it (tried different values of ‘#encoding:’  
without success). Any clue ?

Many thanks in advance  
Best regards

Nicolas

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 10, 2009, 4:25pm UTC](https://www.ruby-forum.com/t/encoding-issue-for-special-characters-on-windows/157307/2 "2009-01-10T16:25:43Z")

</div>

Le 9 janvier 2009 à 10:10, Nicolas G. a écrit :

> There must be an encoding issue at some point in my script but I  
> didn’t manage to fix it (tried different values of ‘#encoding:’  
> without success). Any clue ?

It depends. If you are trying to echo something to the console, you’ll  
have to use CP850.

The character for ä is 228 in the ISO8859-1 [1] encoding that your file  
seems to use, and that corresponds to the õ character in CP850 [2].

Now, if you’re writing something on the screen as a means of control or  
debug while manipulating files, don’t convert your output to CP850 in  
your resulting file ! You’d better stay in ISO, or maybe even in UTF-8,  
depending on what your real goal is (website, internal application,  
database, etc).

Fred  
[1] : [ISO/IEC 8859-1 - Wikipedia](http://en.wikipedia.org/wiki/ISO/IEC_8859-1)  
[2] : [Code page 850 - Wikipedia](http://en.wikipedia.org/wiki/Code_page_850)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 9, 2009, 11:10am UTC](https://www.ruby-forum.com/t/encoding-issue-for-special-characters-on-windows/157307/3 "2009-01-09T11:10:42Z")

</div>

Nicolas G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Running  
> puts ‘ä’  
> in a Ruby script gives  
> ‘õ’  
> as an output, whereas the same code in irb gives  
> ‘ä’
> 
> There must be an encoding issue at some point in my script but I  
> didn’t manage to fix it (tried different values of ‘#encoding:’  
> without success). Any clue ?

I use emacs. In emacs, you’d just put:

#!/usr/bin/ruby

# -_- coding:utf-8 -_-

puts  
“ä”  
to have the script encoded in utf-8 and therefore outputing an utf-8  
byte stream.  
Then of course, you have to have an utf-8 terminal:

[pjb@simias :0.0 tmp]$ chmod 755 test.rb  
[pjb@simias :0.0 tmp]$ export LC\_CTYPE=en\_US.UTF-8  
[pjb@simias :0.0 tmp]$ ./test.rb  
ä  
[pjb@simias :0.0 tmp]$ cat test.rb  
#!/usr/bin/ruby

# -_- coding:utf-8 -_-

puts  
“ä”[pjb@simias :0.0 tmp]$

Notice that in irb, with an utf-8 terminal, “ä”.length == 2

Of course, you can choose to use iso-8859-1 or iso-8859-15, just  
substitute utf-8.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 13, 2009, 1:20pm UTC](https://www.ruby-forum.com/t/encoding-issue-for-special-characters-on-windows/157307/4 "2009-01-13T13:20:33Z")

</div>

On 10 jan, 16:24, “F. Senault” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It depends. If you are trying to echo something to the console, you’ll  
> have to use CP850.
> 
> The character for ä is 228 in the ISO8859-1 [1]encodingthat your file  
> seems to use, and that corresponds to the õ character in CP850 [2].
> 
> Now, if you’re writing something on the screen as a means of control or  
> debug while manipulating files, don’t convert your output to CP850 in  
> your resulting file ! You’d better stay in ISO

Hi and sorry for the delay,

You were right. The screen output was the only one concerned by the  
issue. The result in the filesystem was allright. So everything is  
working as expected since I have no need to display the filenames once  
in production.

Thanks to both of you
