[Numpy-discussion] Re: [Fwd: Re: _byteorder question]

Travis Oliphant oliphant.travis at ieee.org
Tue Jan 17 15:18:01 EST 2006


Christopher Hanley wrote:

> Hi Travis,
>
> This message is completely unrelated to the fromfile discussion.  I 
> have forwarded your previous message to Todd for clarification of our 
> needs.
>
> I have a question regarding byteorder in numpy.  In pyfits, I have a 
> need to change the "sense" of a byteorder flag on an array without 
> actually performing the byteswap operation.  I cannot find a mechanism 
> in numpy to actually do this.  Does one exist?
>
Yes,  you want to change the array data-type object (which holds 
byte-order information).  Previously NOTSWAPPED was a flag on the array 
itself which could have been toggled similarly to Numarray.  Now, 
however, byte-order information is a property of the data-type object 
itself.

On SVN version of NumPy (where the data-type object is now a data-type 
descriptor --- instead of the typeobject of the array scalar), you use 
either

a.dtype = a.dtype.newbyteorder() 

# swaps byteorder (you can also specify 'big', 'little', or 'native')

or if you want a new view with the new byteorder then,

b = a.view(a.dtype.newbyteorder())


Notice:

a = arange(5)
b = a.view(a.dtype.newbyteorder('swap'))
print a
print b
print a.tostring() == b.tostring()

This results in
[0 1 2 3 4]
[       0 16777216 33554432 50331648 67108864]
True

Notice that the actual data is exactly the same...

For fun, try adding 1 to the b array and notice how the a array is 
changed :-)

-Travis






More information about the NumPy-Discussion mailing list