[Numpy-discussion] Controlling endianness of ndarray.tofile()

gary ruben gruben@bigpond.net...
Tue Jun 21 05:37:10 CDT 2011


Hi Ben,
based on this example
<https://bitbucket.org/lannybroo/numpyio/src/a6191c989804/numpyIO.py>
I suspect the way to do it is with numpy.byteswap() and numpy.tofile()
>From <http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.byteswap.html>
we can do

>>> A = np.array([1, 256, 8755], dtype=np.int16)
>>> map(hex, A)
['0x1', '0x100', '0x2233']
>>> A.tofile('a_little.bin')
>>> A.byteswap(True)
array([  256,     1, 13090], dtype=int16)
>>> map(hex, A)
['0x100', '0x1', '0x3322']
>>> A.tofile('a_big.bin')

Gary

On Tue, Jun 21, 2011 at 6:22 PM, Ben Forbes <bdforbes@gmail.com> wrote:
> Hi,
>
> On my system (Intel Xeon, Windows 7 64-bit), ndarray.tofile() outputs
> in little-endian. This is a bit inconvenient, since everything else I
> do is in big-endian. Unfortunately, scipy.io.write_arrray() is
> deprecated, and I can't find any other routines that write pure raw
> binary. Are there any other options, or perhaps could tofile() be
> modified to allow control over endianness?
>
> Cheers,
> Ben
>
> --
> Benjamin D. Forbes
> School of Physics
> The University of Melbourne
> Parkville, VIC 3010, Australia
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list