0

I have a binary file which is specified as "the file contains four fields. Each field is preceded by a 4-byte integer field that specifies its length."

What are the modules, that I need to take a look at in order to be able to read the four segments individually? I tried to open the file in binary mode and read the first 4 elements to get the size of the first field like so:

with open('file', 'rb') as in_file:
    lengthFirstField = in_file.read()[0:4]
    print(lengthFirstField)

But I guess that's wrong because I get four \x00 values which equals 0?

What is the proper way to do something like this?

4
  • 1
    Maybe the first field's length is just 0. Commented Jun 23, 2020 at 21:44
  • FYI Seeking recommendations for books, tools, software libraries, and more is likely to get closed. Commented Jun 23, 2020 at 21:48
  • You can use the struct module to convert 4 bytes into an integer. You'd need to look at the spec of your binary file to see what a length of zero means. Commented Jun 23, 2020 at 21:51
  • The struct module should be sufficient Commented Jun 23, 2020 at 21:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.