Endianness conversion

Dan Sommers me at privacy.net
Sun Feb 25 10:07:16 EST 2007


On Sat, 24 Feb 2007 17:27:12 +0000, Toby wrote:

> Dan Sommers wrote:
>> You could try the struct module.  If your input comes in fixed sized
>> chunks, just call struct.unpack and struct.pack once per chunk.
> 
> Thanks, but it was a bit awkward to use for big chunks.

def swapper( bytestring ):
    someHs = len( bytestring ) / 2 * "H" # could check for odd-lengths?
    unpackfmt = "<" + someHs
    packfmt = ">" + someHs
    return struct.pack( packfmt, *struct.unpack( unpackfmt, bytestring )

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>
Atoms are not things. -- Werner Heisenberg.




More information about the Python-list mailing list