Using decompression to write a byte array with hexadecimal characters?

I want to take the value as:

ff0000

and turn it into a byte array containing these hexadecimal values:

\xff\x00\x00

I do not understand how to do this using str.unpack

+3
source share
2 answers
"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.

+4
source

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

"ff0000".gsub(/../) { |match| match.hex.chr }
+3
source

Source: https://habr.com/ru/post/1720207/


All Articles