[Numpy-discussion] byteswapping a complex scalar

Michael Droettboom mdroe at stsci.edu
Wed Oct 7 11:28:59 EDT 2009


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?

Mike

-- 
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA




More information about the NumPy-Discussion mailing list