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

Ben Forbes bdforbes@gmail....
Tue Jun 21 06:15:55 CDT 2011


Thanks Gary, that works. Out of interest I timed it:

http://pastebin.com/HA4Qn9Ge

On average the swapping incurred a 0.04 second penalty (compared with
1.5 second total run time) for a 4096x4096 array of 64-bit reals. So
there is no real penalty.

Cheers,
Ben

On Tue, Jun 21, 2011 at 8:37 PM, gary ruben <gruben@bigpond.net.au> wrote:
> 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
>>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Benjamin D. Forbes
School of Physics
The University of Melbourne
Parkville, VIC 3010, Australia


More information about the NumPy-Discussion mailing list