DEV Community

Masato Ohba
Masato Ohba

Posted on

Convert emoji and codepoints each other in Ruby

The tips below are what I've learned while implementing a gem, github_reactions, to summarize reactions on GitHub issues and pull requests.

Get codepoints from emoji

"๐Ÿ‘".unpack("U*")
=> [128077]

"๐Ÿ‘".codepoints
=> [128077]

# Convert to hexadecimal
"๐Ÿ‘".each_codepoint.map {|n| n.to_s(16) }
=> ["1f44d"]
Enter fullscreen mode Exit fullscreen mode

Get emoji from codepoints

[128077].pack("U*")
=> "๐Ÿ‘"

0x1f44d.chr('UTF-8')
=> "๐Ÿ‘"

"\u{1f44d}"
=> "๐Ÿ‘"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)