Changing endian format

David M. Wilson at
Sat Jan 24 21:58:50 CET 2004

Amit Gaur <amitgaur at> wrote...

> Hi newbie to python here,
> I have a binary file and i need to change the endian format..little to big as well as vice versa..could anyone help me out.

If you use the struct module to read the file, this appears to be
handled for you.

Example (pack the value 1234 into an unsigned long in little endian,
then big endian format):

   py> import struct
   py> struct.pack('<L', 1234)
   py> struct.pack('>L', 1234)

See also 'struct.unpack', 'socket.htons', and 'socket.htonl'.

More information about the Python-list mailing list