Human readable binary data using Python

My job requires me to do mathematical modeling whose parameters come from a binary file. The simulator can read such a binary file with no problem.

However, I need to look inside the binary to make sure the parameters are what I need for them, and I cannot do this.

I would like to write a script in Python that will allow me to read in a binary file, look for the parameters that I care about, and show what their values ​​are.

What I know about the binary:

It is plain text (as opposed to an image or soud file). There is a piece of code that can "dump" a file in a readable format: if I open this dump in Emacs, I find things like:

CENTRAL_BODY = 'SUN'

      

The entire file is just a series of similar instructions. I could use this dump code, but I prefer Python to do it.

This seems to be a very trivial question and I apologize for not knowing better. I thought I was an experienced programmer!

Many thanks.

+2


source to share


4 answers


It looks like this dump program is already doing what you want: interpreting the binary. I suppose my approach would be to write a python program that can take a dump file, extract the parameters you want, and display them.

Then parse it like this:

myparms.py:

import sys

d = {}
for line in sys.stdin:
    parts = line.split("=",2)
    if len(parts) < 2:
        continue
    k = parts[0].strip()
    v = parts[1].strip()
    d[k] = v

print d['CENTRAL_BODY']

      



Use it like:

dump parameters.bin | python myparms.py

You didn't mention the platform or give details about the dump'ed format, but this should be the place to start.

+1


source


You can read the contents of the file in a line into memory:

thedata = open(thefilename, 'rb').read()

      

and then find the line in it:

where = thedata.find('CENTRAL_BODY')

      



and finally cut off the part you care about:

thepart = thedata[where:where+50]  # or whatever length

      

and display it as you wish (for example, find the string value by specifying thepart

an an sign =

, then the first next quote, then the next quote after that).

+4


source


If it's a binary, you will need to use the struct module. You will need to know how the data is formatted in the file. If this is not documented, you will have to redesign it.

Do you have the source code for another dumping program? You can just port this to Python

We can probably help you if we can see what the binary looks like and the corresponding dump <

0


source


You must know the format in which the data is stored; there is simply no way.

If it doesn't have written specifications, try opening it in a hex editor and examining the format using a text dump as a reference. If you can get the source code for a tool that generates text dumps, that will help you a lot.

Be aware that data can be scrambled in some way, like rot13.

0


source







All Articles