To and from hex nibbles

Hello, I am converting to and from hex nibbles using the code below. Is
there a smarter way for this?

To hex nibbles:
‘testing 123’.scan(/./).collect { |i| i.unpack(‘C’)[0].to_s(16) }.join
=> “74657374696e6720313233”

and back to binary:

“74657374696e6720313233”.scan(/…/).collect { |i| i.to_i(16)
}.pack(‘C*’)
=> “testing 123”

Thanks,

/D

From: daniel åkerud [mailto:[email protected]]

‘testing 123’.scan(/./).collect { |i| i.unpack(‘C’)[0].to_s(16) }.join

=> “74657374696e6720313233”

and back to binary:

“74657374696e6720313233”.scan(/…/).collect { |i| i.to_i(16)

}.pack(‘C*’) # => “testing 123”

s=‘testing 123’.unpack(“H*”)[0]
#=> “74657374696e6720313233”

s.scan(/…/).map{|i| i.to_i(16)}.pack(“C*”)
#=> “testing 123”

kind regards -botp

s.scan(/…/).map{|i| i.to_i(16)}.pack(“C*”)

#=> “testing 123”

sorry, i missed dblack’s scanf

s.scanf("%2x"s.size).pack("C")
#=> “testing 123”

kind regards -botp