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

Neal Becker ndbecker2@gmail....
Wed Jul 29 09:20:19 CDT 2009


Stéfan van der Walt wrote:

> 2009/7/29 Zachary Pincus <zachary.pincus@yale.edu>:
>>> Does numpy have functions to convert between e.g. an array of uint32
>>> and
>>> uint8, where the uint32 array is a packed version of the uint8 array
>>> (selecting little/big endian)?
>>
>>
>> You could use the ndarray constructor to look at the memory differently:
>>
>> In : a = numpy.arange(240, 260, dtype=numpy.uint32)
>> In : a
>> Out:
>> array([240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252,
>> 253, 254, 255, 256, 257, 258, 259], dtype=uint32)
>>
>> In : b = numpy.ndarray(shape=(len(a)*4,), dtype=numpy.uint8, buffer=a)
>>
>> In : b
>> Out:
>> array([240,   0,   0,   0, 241,   0,   0,   0, 242,   0,   0,   0, 243,
>> 0,   0,   0, 244,   0,   0,   0, 245,   0,   0,   0, 246,   0,
>> 0,   0, 247,   0,   0,   0, 248,   0,   0,   0, 249,   0,   0,
>> 0, 250,   0,   0,   0, 251,   0,   0,   0, 252,   0,   0,   0,
>> 253,   0,   0,   0, 254,   0,   0,   0, 255,   0,   0,   0,   0,
>> 1,   0,   0,   1,   1,   0,   0,   2,   1,   0,   0,   3,   1,
>> 0,   0], dtype=uint8)
>>
>>
>> I assume for selecting little/big-endian going the other way, you
>> could use the other methods of specifying dtypes that allow for byte-
>> order descriptors. (Like dtype objects or the format strings.)
> 
> Something like:
> 
> In [17]: np.dtype(np.int32).newbyteorder('>')
> Out[17]: dtype('>i4')
> 
> In [18]: dt = np.dtype(np.int32).newbyteorder('>')
> 
> In [19]: x = np.array([123, 456, 789], dtype=dt)
> 
> In [20]: x.view(np.uint8)
> Out[20]: array([  0,   0,   0, 123,   0,   0,   1, 200,   0,   0,   3,
>  21], dtype=uint8)
> 
> Regards
> Stéfan

Can 'view' switch byteorder?  Doesn't seem to work:

import numpy as np

a = np.arange(10, dtype=np.uint32)

b1 = a.view (np.dtype(np.uint32).newbyteorder('<'))

c1 = b1.view(np.uint8)

b2 = a.view (np.dtype(np.uint32).newbyteorder('>'))

c2 = b2.view(np.uint8)

print c1
print c2

[0 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 5 0 0 0 6 0 0 0 7 0 0 0 8 0 0 0 9
 0 0 0]
[0 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 5 0 0 0 6 0 0 0 7 0 0 0 8 0 0 0 9
 0 0 0]




More information about the NumPy-Discussion mailing list