[Numpy-discussion] Functions to pack/unpack bytes?

Pauli Virtanen pav@iki...
Wed Jul 29 09:44:21 CDT 2009


Wed, 29 Jul 2009 10:20:19 -0400, Neal Becker kirjoitti:
[clip]
> Can 'view' switch byteorder?  Doesn't seem to work:
[clip]

I think not: view only alters the dtype, ie., the interpretation of the 
block of memory. It does not physically reorder the data in memory, which 
is necessary for unpacking to a different byte order.

The reliable way to unpack seems to be

	np.asarray(a, dtype='<u4').view('u1')

which avoids copies when possible. Of course, you could do

	if a.dtype.newbyteorder('<') != a.dtype:
	    a.byteswap(True)
        b = a.view('u1')

to byte-swap in-place to little-endian order.

-- 
Pauli Virtanen



More information about the NumPy-Discussion mailing list