Skip to content

Instantly share code, notes, and snippets.

@kddnewton
Created December 1, 2022 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kddnewton/4be93efd5aafe0e614de9a2680a5b9c4 to your computer and use it in GitHub Desktop.
Save kddnewton/4be93efd5aafe0e614de9a2680a5b9c4 to your computer and use it in GitHub Desktop.
Emoji encoding
def c2e(string) = string.chars.map { |char| [char.ord + 128000].pack("U") }.join
def e2c(string) = string.unpack("U*").pack("c*")
c2e("Hello, world!")
# => "πŸ‘ˆπŸ‘₯πŸ‘¬πŸ‘¬πŸ‘―πŸ¬πŸ πŸ‘·πŸ‘―πŸ‘²πŸ‘¬πŸ‘€πŸ‘"
e2c("πŸ‘ˆπŸ‘₯πŸ‘¬πŸ‘¬πŸ‘―πŸ¬πŸ πŸ‘·πŸ‘―πŸ‘²πŸ‘¬πŸ‘€πŸ‘")
# => "Hello, world!"
e2c(c2e("Hello, world!"))
# => "Hello, world!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment