2

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?

3
  • 1
    Splitting by \t wouldn't work as it might be as well found within the binary data. Commented Jun 2, 2016 at 14:18
  • I didn't think about that. Good I didn't try! Commented Jun 2, 2016 at 14:21
  • you want infine that kind of ascii format ? Commented Jun 2, 2016 at 14:24

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.