How to save string to a file as UTF-8

P

pierr

Bali said:
Below code:

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)
 
B

Bali Bali

pierr said:
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?
 
A

Axel

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"
 
B

Brian Candler

Bali said:
Below code:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top