Skip to content

Instantly share code, notes, and snippets.

@O-I
Last active November 28, 2018 18:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save O-I/6758583 to your computer and use it in GitHub Desktop.
Save O-I/6758583 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!

@clockworkpc
Copy link

For some reason I have to use a smaller range than the one you give. Otherwise it works great!
Ruby 2.4 (installed from RBENV)
Ubuntu 16.04

def get_unicode(char)
  (0..55295).each do |pos|
    chr = ""
    chr << pos
    if chr == char
      puts "This is the unicode of #{char}: #{pos.to_s(16)}"
    end
  end
end

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