I'm trying to use Python read a binary file. The file is in LSB mode. I import the struct module and use unpack like this:
f=open(sys.argv[1],'rb')
contents= unpack('<I',f.read(4))[0]
print contents
f.close()
The data in the file is 0XC0000500 in LSB mode, and the actual value is 0X000500C0. So you can see the LSB mode's smallest size is per byte.
However, I use a Mac machine, perhaps because of the version of my gcc or machine (I am not for sure. I just read the http://docs.python.org/library/struct.html about the sizeof and sys.bitorder), the result from the above code is X0500C000, so the size of the LSB mode is 2Bytes.
How should I solve this problem?
I will keep digging no matter this question is answered or not, and I will update if I ever get something.
ps: The data file is an ELF file for a 32-bit machine.
pps: Since I am going to read a huge amount of data, and this is a general problem in the reading, so the manual way is not the best for me. Question is still open for answers.
ppps: < means "little-endian,standard size (16 bit)" Now I read this...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…