[Numpy-discussion] byteswapping a complex scalar

Travis Oliphant oliphant@enthought....
Thu Oct 8 07:19:14 CDT 2009

On Oct 7, 2009, at 10:28 AM, Michael Droettboom wrote:

> I'm noticing an inconsistency as to how complex numbers are  
> byteswapped
> as arrays vs. scalars, and wondering if I'm doing something wrong.
>>>> x = np.array([-1j], '<c8')
>>>> x.tostring().encode('hex')
> '00000000000080bf'
> # This is a little-endian representation, in the order (real, imag)
> # When I swap the whole array, it swaps each of the (real, imag) parts
> separately
>>>> y = x.byteswap()
>>>> y.tostring().encode('hex')
> '00000000bf800000'
> # and this round-trips fine
>>>> z = np.fromstring(y.tostring(), dtype='>c8')
>>>> assert z[0] == -1j
> # When I swap the scalar, it seems to swap the entire 8 bytes
>>>> y = x[0].byteswap()
>>>> y.tostring().encode('hex')
> 'bf80000000000000'
> # ...and this doesn't round-trip
>>>> z = np.fromstring(y.tostring(), dtype='>c8')
>>>> assert z[0] == -1j
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> AssertionError
> Any thoughts?

I just checked a fix for this into SVN (tests still need to be added  

I can't currently build SVN on my Mac for some reason (I don't know if  
it has to do with recent changes or not, but I don't have time to  
track it down right now....the error I'm getting is something about  
Datetime array scalar types not being defined which seems related to  
the work Dave and Stefan have been discussing).

It's a small change, though, and should work.


More information about the NumPy-Discussion mailing list