Endianness conversion

Toby etatoby at gmail.com
Sat Feb 24 12:27:12 EST 2007


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.

I ended up writing my own byteswapper in Pyrex:


def swapbytes(data):
  "Swap every two bytes of a even-sized python string, in place"
  cdef int i
  cdef char t, *p
  p = data
  for i from 0 <= i < len(data) / 2:
    t = p[0]
    p[0] = p[1]
    p[1] = t
    p = p + 2


Toby



More information about the Python-list mailing list