2

I'm trying to parse a binary file, and I can't quite figure out how to use BinData properly.

The way the binary is set up is like such:

  1. first 4 bytes (UINT32) represent the length of the property name.
  2. next 8 * length represent the property name (as a string).
  3. next 4 bytes (UINT32) represent the length of the property type.
  4. next 8 * length represent the property type (as a string).
  5. next 8 bytes (UINT64) represent the length of the data.
  6. after this the data could be any number of bytes (depending on what type of data it is, could be int (4), string (4 * len), float (4), or an array).

After this the process repeats with the next property.

I guess my questions are:

  1. When I call MyBinDataClass.read() how do I feed it the correct portion to read and not the whole file, and since I don't know how long each property really is (they vary by type), how could I divide it properly?

  2. How do I make a single BinData handle the different property types?

Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
Fianite
  • 319
  • 2
  • 9
  • What have you tried so far? The BinData README seems like it would get you off to a good start. At the very least it shows pretty clearly how to accomplish (1) with the `uint32` method. And the wiki linked to from the README has tons of info. – Jordan Running Jun 07 '16 at 03:55
  • I updated my original post to better define my actual questions. – Fianite Jun 07 '16 at 04:10
  • When you say "next 8 * length" do you mean 8 bits or 8 bytes? This would be a lot easier if you gave us a sample of the data you're working with. – Jordan Running Jun 07 '16 at 05:12
  • 1
    Between the [wiki](https://github.com/dmendel/bindata/wiki) and the [examples](https://github.com/dmendel/bindata/tree/master/examples) I think you ought to be able to figure out how to get started. There are only eight pages of substance in the wiki. It specifically mentions skipping over unused data and handling type-length-value fields. You don't need to know how to solve every part of your problem before you start writing code. I think you could solve #1–5 with little more than the README and some trial-and-error. Do that and worry about #6 later, when you're more familiar with the tool. – Jordan Running Jun 07 '16 at 05:30
  • Hey Jordan, I took your advice and went through the wiki for a bit and I think I figured out most of it! However, I'm really having trouble with the choices aspect. I can't figure out how to store the actual data because how I read the data depends on what the type is. [I made a new question thread about the topic here](http://stackoverflow.com/questions/37684026/structuring-bindata-records-in-ruby). – Fianite Jun 07 '16 at 17:10

0 Answers0