How do I convert a UCS2 string into UTF8?

前端 未结 3 352
野趣味
野趣味 2021-01-14 14:48

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

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 15:39

    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
    

提交回复
热议问题