Endianness conversion

T

Toby

As part of a program I'm writing, I need to save to disk big amounts of
data (hundreds of MB, in 8kB chunks) swapping every couple of bytes.

I obviously cannot do it in a Python loop.

Is there a function I could use in the standard library, or do I have to
write my own C extension?


Toby
 
D

Dan Sommers

As part of a program I'm writing, I need to save to disk big amounts of
data (hundreds of MB, in 8kB chunks) swapping every couple of bytes.

I obviously cannot do it in a Python loop.

Is there a function I could use in the standard library, or do I have to
write my own C extension?

You could try the struct module. If your input comes in fixed sized
chunks, just call struct.unpack and struct.pack once per chunk.

HTH,
Dan
 
T

Toby

Dan said:
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
 
D

Daniel Harding

As part of a program I'm writing, I need to save to disk big amounts of
data (hundreds of MB, in 8kB chunks) swapping every couple of bytes.

I obviously cannot do it in a Python loop.

Is there a function I could use in the standard library, or do I have to
write my own C extension?

Try the using the array module. array objects provide a byteswap
method which reverses endianness.

-Daniel
 
D

Dan Sommers

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 )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top