Reputation: 284
I want to pack a string containing "00000000"
.
If i do "00000000".to_a.pack('H*')
I get an error
in 'system': string contains null byte (ArgumentError)
I need to send this as a hex string to a microprocessor. A sample could be 0x81 0x00 0x00 0x21
Upvotes: 2
Views: 2334
Reputation: 132902
Try this:
input = "A000B0"
output = []
until input.empty?
output << input[0, 2].to_i(16)
input = input[2..-1]
end
puts output.pack('C*').inspect # => "\xA0\x00\xB0"
Upvotes: 1
Reputation: 66837
There's a pack
directive for null bytes: x
.
>> ["a", "b"].pack("HxH") #=> "\xA0\x00\xB0"
Upvotes: 3