How can I get the char for a given UTF-8 code in Ruby 2.1

739 Views Asked by At

I was wondering if there is a way to get the character for a given UTF-8 code ?

E.g.:

1103 = > "я"(russian letter)
2

There are 2 best solutions below

0
On

Using Array#pack with U directive (UTF-8 character):

[1103].pack('U')
# => "я"
0
On

Another approach is "\u{hex}", e.g. "\u{4355}". This syntax accepts only hex numbers, not decimal. Syntax U+184B is the most commonly used one for referencing Unicode characters.