How do I convert a UCS2 string into UTF8?

微笑、不失礼 提交于 2019-12-01 06:30:44

问题


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


回答1:


You should look into iconv, which is part of the Ruby standard library. It is designed for this task.

Specifically,

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

should handle the conversion.




回答2:


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



回答3:


With Ruby 1.9:

string.encode("utf-8")

If the string encoding is not known, you may need to set it first:

string.force_encoding("utf-16be").encode("utf-8") # Big-endian
string.force_encoding("utf-16le").encode("utf-8") # Little-endian


来源:https://stackoverflow.com/questions/1033104/how-do-i-convert-a-ucs2-string-into-utf8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!