Convert ByteString into numpy array of 1s and 0s

Question:

I am wanting to turn a bytestring, for example b'xedx07bx87S.x866^x84x1ex92xbfxc5rx8c' into a numpy array of 1s and 0s (i.e. the binary value of this bytestring as an array of binary values).

How would I go about doing this?

I tried using np.fromstring and np.frombuffer but neither did what I wanted.

Asked By: SamTheProgrammer

||

Answers:

Use numpy.unpackbits. Per the docs:

Unpacks elements of a uint8 array into a binary-valued output array.

import numpy as np

b = b'xedx07bx87S.x866^x84x1ex92xbfxc5rx8c'

bits_array = np.unpackbits(np.frombuffer(b, dtype=np.uint8))
print(bits_array)

outputs

[1 1 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0 1 1 0 0 0 1 0 1 0 0 0 0 1 1 1 0 1 0 1 0
 0 1 1 0 0 1 0 1 1 1 0 1 0 0 0 0 1 1 0 0 0 1 1 0 1 1 0 0 1 0 1 1 1 1 0 1 0
 0 0 0 1 0 0 0 0 0 1 1 1 1 0 1 0 0 1 0 0 1 0 1 0 1 1 1 1 1 1 1 1 0 0 0 1 0
 1 0 0 0 0 1 1 0 1 1 0 0 0 1 1 0 0]
Answered By: Brian

I don’t believe numpy gives you a way to do this directly.

You can do this in several steps:

x = np.frombuffer(data, dtype=np.ubyte)
y = np.array([1, 2, 4, 8, 16, 32, 64, 128])

z = np.sign(x[:,None] & y[None,:])

This now gives you a 16×8 array of 0’s and 1s with your data. You can resize it if you want flat data.

z.resize(len(data) * 8)
Answered By: Frank Yellin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.