tags:

views:

36

answers:

1

I want to split data to chunks of let's say 8154 byte:

data = Zlib::Deflate.deflate(some_very_long_string)

What would be the best way to do that?

I tried to use this:

chunks = data.scan /.{1,8154}/

...but data was lost! data had a size of 11682, but when looping through every chunk and summing up the size I ended up with a total size of 11677. 5 bytes were lost! Why?

Thank you!

+3  A: 

Regexps are not good way to parse binary data. Try to use bytes and each_slice. Also you can use pack 'C*' to pack byte arrays into strings, if you want.

irb(main):110:0> data = File.open('sample.gif','rb'){|f|f.read}
=> "GIF89a\r\x00\r........."

irb(main):112:0> 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"
...........
Nakilon
this worked great! thank you! :)
Lennart