views:

973

answers:

2

How to convert a string that is in UCS2 (2 bytes per character) into a UTF8 string in ruby?

+3  A: 

You should look into iconv (http://www.ruby-doc.org/stdlib/libdoc/iconv/rdoc/index.html) part of the ruby standard library. It is designed for this task.

Specifically,

 Iconv.iconv("utf-16", "utf-8", str).first

should handle the conversion.

Ben Hughes
+1  A: 

Because chars in most cases string in UCS2 encoding can be represented as UTF-16 string (in UTF-16 char with codes bigger than 0x10000 is rarely used) I think use of Iconv is better way to convert strings. Sample code:

require 'iconv'

ic = Iconv.new 'UTF-8', 'UTF-16'
utf8string = ic.iconv ucs2string
Eskat0n