Changing endian format
Jeff Epler
jepler at unpythonic.net
Sat Jan 24 11:26:14 EST 2004
On Sat, Jan 24, 2004 at 06:56:16AM -0800, Amit Gaur 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.
> thanks
The struct module supports an endian flag.
As an example, suppose my data format is
AAAA BB CC
a 4-byte int followed by two 2-byte ints, and my task is to take a
big-endian version of the data and convert it to small-endian:
import struct
def convert(format, s, e1, e2):
l = struct.unpack(e1 + format, s)
return struct.pack(e2 + format, *l)
>>> convert("LHH", "12345678", ">", "<")
'43216587'
See the struct module documentation for more information.
Jeff
More information about the Python-list
mailing list