0

I have a Ruby script that generates an ANSI file.

I want to convert the file to UTF8.

What's the easiest way to do it?

TheDude
  • 3,796
  • 2
  • 28
  • 51
HelloWorld
  • 7,156
  • 6
  • 39
  • 36

3 Answers3

2

If your data is between ascii range 0 to 0x7F, its valid UTF8, so you don't need to do anything.

Or, if there is characters above 0x7F, you could use Iconv

text=Iconv.iconv('UTF-8', 'ascii',text)
YOU
  • 120,166
  • 34
  • 186
  • 219
  • I solution my problem. My code is: text=Iconv.iconv('UTF-8', 'gb2312',text) – HelloWorld Apr 28 '10 at 05:59
  • 1
    Oh, ok, I didn't know your encoding was 'gb2312', so I put 'ascii' as example. You should have asked gb2312 to UTF8 in question. but anyway, glad to know its works. – YOU Apr 28 '10 at 06:01
  • Iconv has been deprecated in 1.9.3 http://stackoverflow.com/questions/8148762/iconv-deprecation-warning-with-ruby-1-9-3 Try String#encode instead. – Casey Robinson Feb 07 '13 at 00:18
1

The 8-bit Unicode Transformation Format (UTF-8) was designed to be backwards compatible with the American Standard Code for Information Interchange (ASCII). Therefore, by definition, any valid ASCII sequence is also a valid UTF-8 sequence. For more information, read the UTF FAQ and Unicode FAQ.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
0

Any ASCII file is a valid UTF8 file, going by your Q's title, so no conversion is needed. I don't know what a UIF8 file is, going by your Q's text, so different from its title.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395