I have these values from a unicode database but I'm not sure how to translate them into the human readable form. What are these even called?
Here they are:
U+2B71F
U+2A52D
U+2A68F
U+2A690
U+2B72F
U+2B4F7
U+2B72B
How can I convert these to there readable symbols?
How about:
# Using pack
puts ["2B71F".hex].pack("U")
# Using chr
puts (0x2B71F).chr(Encoding::UTF_8)
In Ruby 1.9+ you can also do:
puts "\u{2B71F}"
I.e. the \u{}
escape sequence can be used to decode Unicode codepoints.
The unicode symbols like U+2B71F
are referred to as a codepoint
.
The unicode system defines a unique codepoint
for each character in a multitude of world languages, scientific symbols, currencies etc. This character set is steadily growing.
For example, U+221E
is infinity.
The codepoints
are hexadecimal numbers. There is always exactly one number defined per character.
There are many ways to arrange this in memory. This is known as an encoding
of which the common ones are UTF-8
and UTF-16
. The conversion to and fro is well defined.
Here you are most probably looking for converting the unicode codepoint
to UTF-8
characters.
codepoint = "U+2B71F"
You need to extract the hex part coming after U+
and get only 2B71F
. This will be the first group capture. See this.
codepoint.to_s =~ /U\+([0-9a-fA-F]{4,5}|10[0-9a-fA-F]{4})$/
And you're UTF-8 character will be:
utf_8_character = [$1.hex].pack("U")
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With