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...
repr(f.read(4))and the expected output as a decimal integer? - jfsdcbato0xabcdseems unusual (see updated answer. Could you includerepr(f.read(4))in the question (don't type it just copy-paste whatever you see) and the expected output as a decimal integer (as123, not0x7bto avoid any confusion about the byte order)? - jfs