DEV Community

Masato Ohba
Masato Ohba

Posted on

6 3

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay