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

Neal Becker ndbecker2@gmail....
Fri Jul 31 12:47:50 CDT 2009


Travis Oliphant wrote:

> Doesn't .view work for that?
> 
> Travis
> 
> 
> Sent from my iPhone
> 
> On Jul 29, 2009, at 6:57 AM, Neal Becker <ndbecker2@gmail.com> wrote:
> 
>> Neal Becker wrote:
>>
>>> 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)?
>>
>> I've been thinking about making a ufunc (in python) to do this, but
>> have no
>> idea how to make a ufunc for a function that consumes 4 consecutive
>> values
>> of it's input and produces an output array of 1/4 the size of the
>> input.  Is
>> that possible?
>>
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