How to save string to a file as UTF-8

Below code:

···

--
s1 = "good"

File.new("test.xml", "w+") << s1
--

It always give a file ANSI encoded. How can I get it UTF-8 encoded?

Thanks.
--
Posted via http://www.ruby-forum.com/.

Bali Bali wrote:

Below code:
--
s1 = "good"

File.new("test.xml", "w+") << s1
--

It always give a file ANSI encoded. How can I get it UTF-8 encoded?

If your ruby version < "1.9"
$KCODE = "UTF8"
else
  #!/usr/bin/env ruby
  # encoding: utf-8 <--- this line
end

(NOTES : above code is not legal ruby code)

···

--
View this message in context: http://www.nabble.com/How-to-save-string-to-a-file-as-UTF-8-tp24731209p24731698.html
Sent from the ruby-talk mailing list archive at Nabble.com.

Bali Bali wrote:

Below code:
--
s1 = "good"

File.new("test.xml", "w+") << s1
--

It always give a file ANSI encoded. How can I get it UTF-8 encoded?

What do you mean by "ANSI encoded"? Do you mean "ASCII"?

The text "good" is exactly the same in both ASCII and UTF-8, so how do
you know it is one and not the other? Can you give a different example
which shows the problem?

Use "hexdump -C test.xml" to show the exact content of the file, byte by
byte.

···

--
Posted via http://www.ruby-forum.com/\.

pierr wrote:

Bali Bali wrote:

If your ruby version < "1.9"
$KCODE = "UTF8"
else
  #!/usr/bin/env ruby
  # encoding: utf-8 <--- this line
end

(NOTES : above code is not legal ruby code)

pierr, Thanks! But looks like it is not working for me.

Code v2.0

···

--
$KCODE = "UTF-8"

s1 = "good"

File.new("test.xml", "w+") << s1
--

It still creates a ANSI file. My Ruby version:
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

It is windows release which should not be related to the issue.

Any idea?
--
Posted via http://www.ruby-forum.com/\.

You can try

    require 'iconv'
    myEncoding = 'iso-8859-15' # or 'CP1252' or what you have
    s1 = 'ü'
    s1 = Iconv.new('utf-8', myEncoding ).iconv(s1)

Maybe, you will need the "Byte-order mark". That are 3 special Bytes
at the very beginning of your utf8-file:
wikipedia.org > utf-8 > "Byte-order mark"