Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What endianness does Python use to write into files?

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

like image 481
e-nouri Avatar asked Dec 25 '22 08:12

e-nouri


2 Answers

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.

like image 190
woot Avatar answered Dec 31 '22 12:12

woot


It uses sys.byteorder. So just:

import sys

if 'little' == sys.byteorder:
     # little
 else:
     # big
like image 21
cchristelis Avatar answered Dec 31 '22 13:12

cchristelis