tags:

views:

148

answers:

2

I want to take a value like:

ff0000

and make it into a byte array containing those hex values:

\xff\x00\x00

I'm not clear on how to do this using str.unpack

+3  A: 

I'm not sure unpack can do this. Try this instead:

"ff0000".gsub(/../) { |match| match.hex.chr }
wdebeaum
+3  A: 
"ff0000".scan(/../).map { |match| match.hex } #=> [255, 0, 0]

or

("ff0000".scan(/../).map { |match| match.hex }).pack('C*') #=> "\377\000\000"

Depending on what format you want it in.

Bob Aman