Changing endian format

David M. Wilson dw-google.com at botanicus.net
Sat Jan 24 15:58:50 EST 2004


Amit Gaur <amitgaur at yahoo.com> 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.

   http://python.org/doc/current/lib/module-struct.html


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

   py> import struct
   py> struct.pack('<L', 1234)
   '\xd2\x04\x00\x00'
   py> struct.pack('>L', 1234)
   '\x00\x00\x04\xd2'


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



More information about the Python-list mailing list