Ruby Hex-Bin conversion
mouse 256 · person cloud · link
Last update
2017-08-21
2017
08-21
« — »

Manual conversion:

1
2
'abc'.each_byte.map{|b| b.to_s(16) }.join   # '616263'
'616263'.scan(/../).map{|x| x.hex.chr}.join # 'abc'

Array#pack and String#unpack:

1
2
3
# pack/unpack methods
'abc'.unpack('H*').first # '616263'
'616263'.scan(/../).map{|i| i.hex}.pack('c*')

Source: AnthonyLewis