Ruby - Unpack irregular binary string

Question

I have an irregular binary file that has been produced by an external program.

The beginning of the file looks like this

"mct_terrain_material\t\xF32\xE1Ao\xAFLA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xBFFrontColor\t\xF32\xE1A\x80\xB7\xCDA\e\xFB\e@\x00\x00\x00\x00/\xA6\x7F?:^V=FrontColor\tg\x10\xECA\x80\xB7\xCDA98\x1C@\xEA\xC7\xD5=\xAA?~?:^V=FrontColor\t(\x97\fB\xC0\xDB6B\x9E\x87a@\x1C\x9CT>\a\x10z?:^V=F....."

As you can see, it has a repetitive pattern:

  • String (unspecified length)
  • tab (\t)
  • 6 floats
  • Repeat

In the ASCII version the first three elements would constitute a line:

mct_terrain_material    2.814988e+01    1.279283e+01    0.000000e+00    0.000000e+00    0.000000e+00    -1.000000e+00
FrontColor  2.814988e+01    2.571460e+01    2.437201e+00    0.000000e+00    9.986295e-01    5.233596e-02

There is no newline in the binary version.

I know how to unpack strings with only one type. In that case I would do:

binstring.unpack("F*")

My first idea was to use binstring.split("\t") and then build one by one starting the unpacking from the second element, but I think that there should be a more elegant solution.

Any idea?


Show source
| ruby   | regex   | binaryfiles   | unpack   2016-06-02 16:06 1 Answers

Answers ( 1 )

  1. 2016-06-02 16:06

    Could you try something along the lines of:

    while string = file.gets("\t") # separator instead of \n
      binary = file.read(6*4) # or whatever size of the float is
      floats = binary.unpack('F*') # or however you unpack this to array
      # do whatever with string and floats
    end
    
◀ Go back