1

I have a binary file (which I've created in C) and I would like to have a look inside the file. Obviously, I won't be able to "see" anything useful as it's in binary. However I do know that contains certain number of rows with numbers in double precision. I am looking for a script to just read some values and print them so I can verify the if they are in the right range. In other words, it would be like doing head or tail in linux on an text file. Is there a way of doing it? Right now I've got something in Python, but it does not do what I want:

CHUNKSIZE = 8192
file = open('eigenvalues.bin', 'rb')
data = list(file.read())
print data

3 Answers 3

3

Use the array module to read homogenous binary-representation numbers:

from array import array

data = array('d')
CHUNKSIZE = 8192
rowcount = CHUNKSIZE / data.itemsize  # number of doubles we find in CHUNKSIZE bytes

with open('eigenvalues.bin', 'rb') as eg:
    data.fromfile(eg, rowcount)

The array.array type otherwise behaves just like a list, only the type of values it can hold is constricted (in this case to float).

Depending on the input data, you may need to add a data.byteswap() call after reading to switch between little and big-endian. Use sys.byteorder to see what byteorder was used to read the data. If your data was written on a platform using little-endianess, swap if your platform uses the other form, and vice-versa:

import sys

if sys.byteorder == 'big':
    # data was written in little-endian form, so swap the bytes to match
    data.byteswap()
Sign up to request clarification or add additional context in comments.

1 Comment

@Seidr: array is a nice complimentary module to struct, if you are dealing with a sequence of just one type of C-standard binary data.
3

You can use struct.unpack to convert binary data into a specific data type.

For example, if you want to read the first double from the binary data. (not tested, but believe this is correct)

struct.unpack("d",inputData[0:7])

http://docs.python.org/2/library/struct.html

Comments

3

You can see each byte of your file represented in unsigned decimal with this shell command:

od -t u1 eigenvalues.bin | less

Should you want to see a particular area and decode floating point numbers, you can use dd to extract them and od -F option to decode them, eg:

dd status=noxfer if=eigenvalues.bin bs=1 skip=800 count=16 | od -F

will show two double precision numbers stored at offset 800 and 808 in the binary file.

Note that according to the Linux tag set to your question, I assume you are using Gnu versions of dd and od.

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.