2

To get bits from bytes:

bytes = bytes([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
bits = numpy.unpackbits(bytes)  

throws this error:
TypeError: Expected an input array of unsigned byte data type
but if I get bytes in this way it works:

bytes = numpy.fromfile(filename, dtype="uint8")

How to solve this error, thanks.

2 Answers 2

1

You have to pass an array with a dtype of unsigned integer:

hex_codes = 0x13, 0x00, 0x00, 0x00, 0x08, 0x00
arr = np.array(hex_codes).astype(np.uint8)
np.unpackbits(arr)

Output:

array([0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0], dtype=uint8)

Btw, you probably know this, but you should not reassign a builtin as you did with bytes.

Sign up to request clarification or add additional context in comments.

Comments

1

bytes should be unsigned array, why not use a numpy unsigned?

bytearray = numpy.array([0x13, 0x00, 0x00, 0x00, 0x08, 0x00],dtype=numpy.uint8)
bits = numpy.unpackbits(bytearray)  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.