[Tutor] newb - Problem reading binary files
Elizabeth Finn
finnfamilyus at yahoo.com
Sat May 12 01:27:07 CEST 2007
This is probably a newbie question, and I apologize for the length – but I have consulted several books / sites and haven’t found a good answer. I need to read a file that is in binary format, and then convert some of the values into integer values. These values can range from 1 to 4 bytes.
First question – is there an easy way to do this? I finally wrote my own little utility to handle multi-byte integers because I couldn’t find a built-in way (except for ord() which works only for single bytes)
def getnum(num_str):
"""
Given a string representing a binary number, return the number.
If string is more than one byte, calculate the number to return.
Assume that each byte is signed magnitude
"""
x = len(num_str)
ans = 0
for i in range( x ):
nextstr = num_str[i:i+1]
ans = ans * 256
ans = ans + ord(nextstr)
return ans
This “brute force” method usually works, but - now here is the other question -sometimes the code does not pick up two full bytes when it is supposed to. I open the file and read a block that I want into a string:
f=open(fname, 'rb')
f.seek(offset, 0)
block = f.read(2000)
Then for each number I pull the bytes from the string, then call getnum() to calculate the number.
test = block[0:1] # 1 byte
test = block[1:4] # 3 bytes
test = block[4:6] # 2 bytes
test = block[20:12] # 2 bytes
test = block[1996:2000] #4 bytes
This method usually works, except that for some of the 2-byte numbers I get only the first byte and first half of the second byte – for instance: 'x06\x33’ comes out as ‘x063’. This is very confusing especially because one 2-byte substring – “00 01” comes out as expected, but “06 52” becomes “065”. Any ideas?
---------------------------------
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070511/1f26df8f/attachment.html
More information about the Tutor
mailing list