Subheader

Lazy Programmer's Blog

Pages

Saturday, March 26, 2016

How to get character's unicode value in Ruby

The solution is to use .ord.to_s(16)

Examples:
'♥'.ord.to_s(16) = 2665
'$'.ord.to_s(16) = 24
'€'.ord.to_s(16) = 20ac
'a'.ord.to_s(16) = 61

Getting unicode value of string:
'my string'.each_char.map{|c| c.ord.to_s(16)}.join

No comments:

Post a Comment