Search code examples
rubyencodingutf-8character-encodingutf-16

Ruby encode UTF-8 string to UTF-16


I want to store the UTF-16 encoding into another variable as UTF-8 string.

1.9.3p194 :117 > str = "سلام"
 => "سلام" 
1.9.3p194 :118 > enc = str.encode("utf-16")
 => "\uFEFF\u0633\u0644\u0627\u0645" 
1.9.3p194 :119 > puts enc
??3D'E
 => nil 

I want to store \uFEFF\u0633\u0644\u0627\u0645 (not ??3D'E) into a UTF-8 string so I can be able to concatenate it with other UTF-8 strings


Solution

  • Use String#inspect:

    str = "سلام"
    # => "سلام"
    enc = str.encode("utf-16")
    # => "\uFEFF\u0633\u0644\u0627\u0645"
    puts enc
    # output: ▒▒3D'E
    # => nil
    puts enc.inspect
    # output: "\uFEFF\u0633\u0644\u0627\u0645"
    # => nil