Home > front end >  Native array.frombytes() (not numpy!) mysterious behavior
Native array.frombytes() (not numpy!) mysterious behavior

Time:04-07

[I cannot use numpy so please refrain from talking about it]

I (apparently naively) thought Python array.frombytes() would read from a series of bytes representing various machine format integers, depending on how you create the array object. On creation you are required to provide a letter type code telling it (or so I thought) the machine type of integer making up the byte stream.

import array

b = b"\x01\x00\x02\x00\x03\x00\x04\x00"
a = array.array('i') #signed int (2 bytes)
a.frombytes(b)
print(a)

array('i', [131073, 262147])

and in the debugger:
array('i', [131073, 262147])
    itemsize: 4
    typecode: 'i'

The bytes in b are a series of little endian int16s (type code = 'i'). Despite being told this, it interpreted the bytes as 4-byte integers. This is Python 3.7.8.

I really need to convert the varying ints into an array (or list) of Python ints to deal with image data coming in byte-streams but which is actually either 16-bit or 32-bit integer, or 64 bit double floating format. What did I miss or do wrong? Or what is the right way to accomplish this?

CodePudding user response:

Note that the documentation doesn't specify the exact size of each type, it specifies the minimum size. Which means it may use a larger size if it wants, probably based on the types in the C compiler that was used to build Python.

Here are all the sizes on my system:

for c in 'bBuhHiIlLqQfd':
    print(c, array(c).itemsize)

b 1
B 1
u 2
h 2
H 2
i 4
I 4
l 4
L 4
q 8
Q 8
f 4
d 8

I would suggest using the 'h' or 'H' type.

  • Related