I’m trying to read some binary datafiles. The datafiles have a “sentinal” integer written in them which is always -1. I thought that I could use this to check if the machine which wrote the data was big-endian or little-endian, but upon some experimentation it seems this isn’t the case. Specifically:
import struct
data=struct.pack('<i',-1)
print (struct.unpack('<i',data)) #(-1,)
print (struct.unpack('>i',data)) #(-1,) ???
The representation of -1 is the same in big and liddle endian (and two’s complement), i.e. (assuming 32 bit)
Check with a value such as
0x01020304, or, if you want to add additional checks,0x0d0aff00(0d0ais the Windows EOL CRLF,ffwill break over 8bit-opaque channels, and00will break null-terminated strings).