When using file.write() with 'wb' flag does Python use big or litte endian, or sys.byteorder value ? how can i be sure that the endianness is not random, I am asking because I am mixing ASCII and binary data in the same file and for the binary data i use struct.pack() and force it to little endian, but I am not sure what happen to the ASCII data !
Edit 1: since the downvote, I'll explain more my question !
I am writing a file with ASCII and binary data, in a x86 PC, the file will be sent over the network to another computer witch is not x86, a PowerPC, witch is on Big-endian, how can I be sure that the data will be the same when parsed with the PowerPC ?
Edit 2: still using Python 2.7
For multibyte data, It follows the architecture of the machine by default. If you need it to work cross-platform, then you'll want to force it.
ASCII and UTF-8 are encoded as a single byte per character, so is it affected by the byte ordering? No.
Here is how to pack little <
or big >
endian:
import struct
struct.pack('<L', 1234)
'\xd2\x04\x00\x00'
struct.pack('>L', 1234)
'\x00\x00\x04\xd2'
You can also encode strings as big or little endian this way if you are using UTF-16, as an example:
s.encode('utf-16LE')
s.encode('utf-16BE')
UTF-8, ASCII do not have endianness since it is 1 byte per character.
It uses sys.byteorder. So just:
import sys
if 'little' == sys.byteorder:
# little
else:
# big
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With