Skip to content

Instantly share code, notes, and snippets.

@naveed-ahmad
Forked from O-I/get_character.rb
Created January 16, 2017 11:52
Show Gist options
  • Save naveed-ahmad/79df9ea40c675e9c8b58a0f146afb904 to your computer and use it in GitHub Desktop.
Save naveed-ahmad/79df9ea40c675e9c8b58a0f146afb904 to your computer and use it in GitHub Desktop.
Ruby Character to Unicode Converter
def get_character(hexnum)
char = ''
char << hexnum.to_i(16)
end
def get_unicode(char)
(0..109_976).each do |pos|
chr = ''
chr << pos
return pos.to_s(16) if chr == char
end
end

Ruby Character to Unicode Converter

(and vice versa)

I 'accidentally' created this while trying to make a string representation of the first million digits of the Champernowne constant. I thought it was pretty nifty, so I'm sharing.

Ever needed to find the Unicode value of some crazy-cool character like, say, ∀? Just

puts get_unicode('∀')

and you'll find its hex value is:

2200

Or if you've always been itching to know what Unicode character is represented by hex value 2752,

puts get_character('2752')

to find out. Go ahead — play with it — you know you want to!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment