Hex encoded ASCII to String in Ruby
My current project required a simple operation to perform that took me some time to find in ruby. I’ll put a copy here so hopefully it’s of use to someone:
[source:ruby]str.scan(/../).each { | tuple | puts tuple.hex.chr }[/source]
So this will convert Hexidecimal tuples into ASCII literals, like follows:
[source:ruby]str=”48656C6C6F”
str.scan(/../).each { | tuple | puts tuple.hex.chr }
H
e
l
l
o
=> ["48", "65", "6C", "6C", "6F"][/source]
As I say, I couldn’t find this anywhere else, so enjoy.