Regular expressions are not a good way to parse binary data. Use bytesand each_sliceto manage bytes. And use pack 'C*'to convert them back to strings for output or debugging:
irb> data = File.open("sample.gif", "rb", &:read)
=> "GIF89a\r\x00\r........."
irb> data.bytes.each_slice(10){ |slice| p slice, slice.pack("C*") }
[71, 73, 70, 56, 57, 97, 13, 0, 13, 0]
"GIF89a\r\x00\r\x00"
[247, 0, 0, 0, 0, 0, 0, 0, 51, 0]
"\xF7\x00\x00\x00\x00\x00\x00\x003\x00"
...........
source
share