Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get unicode by hex string in Ruby [duplicate]

Tags:

ruby

hex

unicode

Given a hex code, for example: 1f60d, how do I find the corresponding unicode (code point) 😍?

like image 810
Juanito Fatas Avatar asked Aug 20 '18 06:08

Juanito Fatas


2 Answers

You could do that by using Array#pack:

["1F60d".to_i(16)].pack("U*")

like image 94
Juanito Fatas Avatar answered Oct 18 '22 20:10

Juanito Fatas


Convert the string to an integer (e.g. via hex) and the integer to a character via chr:

'1f60d'.hex.chr('UTF-8')
#=> "😍"
like image 27
Stefan Avatar answered Oct 18 '22 19:10

Stefan