Converting data to little endian
Fredrik Lundh
fredrik at pythonware.com
Wed Dec 8 03:00:02 EST 1999
Travis Oliphant <olipt at mayo.edu> wrote:
> > 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
a byteswap operation:
>>> import array
>>> a = array.array("i", [1, 2])
>>> a
array('i', [1, 2])
>>> 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>
More information about the Python-list
mailing list