Parsing a binary file in Ruby
-
19-09-2019 - |
Question
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.
Solution
You want String#unpack.
OTHER TIPS
You could also take a look at BitStruct which offers a nice high-level interface for parsing binary data.
Check out the BinData gem, and the alternatives it lists:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow