Hello Daniweb,

I would like to read binary data into floating point numbers. I'd be happy with a 1-D array (or vector) or a 2-D array. Just something I can write to a text file. I attached a small sample of this data. Here is the code I have so far based on googling this topic. I'm using python 2.5.

f = open(inputname, "rb") 
    byte = f.read(1) 
    while byte != "": 
        # Do stuff with byte. 
        byte = f.read(1)
        print byte
        ## num=2bytes  I need a line here that converts bytes to numbers


I am able to read and print the bytes, but how do I convert them to data to append them for writing? The bytes appear very interesting.

I know the data is in raw binary format (with little-endian—Intel PC—byte order), and that two bytes is one value, and that the number of rows 240, and the number of columns 480 (for this test data set).

Any ideas are invited.

Also, Does anyone know how I can change the name of my Daniweb Avatar?

6 Years
Discussion Span
Last Post by woooee

Oops, the 'filex' in the above code is really meant to be 'inputname.'


At one time there was a third party package named BitBuffer to work with binary files. I don't know if it is still around and maintained or not. You can also use array

filename = "binary_number.py"
a_long = array.array('H')
fp = open(filename, 'rb')                                                       
a_long.fromfile(fp, 1)
print a_long
a_long.fromfile(fp, 1)
print a_long

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.