Using unpack to write a byte array with hex characters?

ruby

Solution

"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.

Problem

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`

Original source