How to convert UCS2 string to UTF8?

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

+3
source share
3 answers

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

In particular,

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

must handle the conversion.

+4
source

Since characters in most cases, a UCS2 encoded string can be represented as a UTF-16 string (in UTF-16 char with codes larger than 0x10000 is rarely used) I think that using Iconv is the best way to convert strings. Code example:

require 'iconv'

ic = Iconv.new 'UTF-8', 'UTF-16'
utf8string = ic.iconv ucs2string
+2
source

Ruby 1.9:

string.encode("utf-8")

, :

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

Source: https://habr.com/ru/post/1711093/


All Articles