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.

filex='test.bin'
gq=[]
f = open(inputname, "rb") 
try: 
    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
        #gq.append(num)
 #outfile=open('output.txt','w') 
 #outfile.write(gq)

.

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?

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
ETC
fp.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.