Discussion:
Converting data to little endian
victor_ng
1999-12-07 18:24:12 UTC
Permalink
Hi, I was wondering how to get my data to be represented in little
endian format. I know that I can use the socket functions to convert to
network byte order (big endian), but I need to convert to little endian
(not just the host byte order).

Vic


Sent via Deja.com http://www.deja.com/
Before you buy.
Andrew MacIntyre
1999-12-07 20:26:46 UTC
Permalink
Post by victor_ng
Hi, I was wondering how to get my data to be represented in little
endian format. I know that I can use the socket functions to convert to
network byte order (big endian), but I need to convert to little endian
(not just the host byte order).
module struct?

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: andrew.macintyre at aba.gov.au (work) | Snail: PO Box 370
andymac at bullseye.apana.org.au (play) | Belconnen ACT 2616
Fido: Andrew MacIntyre, 3:620/243.18 | Australia
Fredrik Lundh
1999-12-08 08:00:02 UTC
Permalink
Post by victor_ng
Hi, I was wondering how to get my data to be represented in little
endian format. I know that I can use the socket functions to convert to
network byte order (big endian), but I need to convert to little endian
(not just the host byte order).
One possibility is to use the NumPy extensions and store your data in a
multiarray object. There is a byteswapped method of the multiarray object
that lets you byteswap your data.
footnote: the standard array module also provides
Post by victor_ng
import array
a = array.array("i", [1, 2])
a
array('i', [1, 2])
Post by victor_ng
a.byteswap()
a
array('i', [16777216, 33554432])
There is also a way to check the endianness of your machine (so
you can determine whether or not to byteswap).
from array-example-4.py in the eff-bot guide:

def little_endian():
return ord(array.array("i",[1]).tostring()[0])

</F>

Continue reading on narkive:
Loading...