views:

748

answers:

2

I need to be able to parse a binary file with Ruby. This file contains chunks of data that are found via a header that includes the file offset and length of each chunk.

How do I get the data out correctly? I've been unable so far to seek around in the file based on the offsets I read out since they come out in strings that I don't know how to convert to a format IO#seek understands.

Any help? A general way to convert the offset and length into something usable (like an integer or something) would be helpful.

+2  A: 

You want String#unpack.

hhaamu
That was it. unpack('L*') is what I needed.
Robert Rouse
You should be explicitly using the little-endian or big-endian unpacks instead of native-endian, to keep a bit of portability.
hhaamu
A: 

You could also take a look at BitStruct which offers a nice high-level interface for parsing binary data.