3

I have a numpy binary array like this:

np_bin_array = [0 1 0 0 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 1 1 0 0 0 1 1 0 1 1 0 0 0 1 1 0 1 1 1 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]

It was 8-bit string characters of a word originally, starting from the left, with 0's padding it out.

I need to convert this back into strings to form the word again and strip the 0's and the output for the above should be 'Hello'.

Thanks for your help!

0

4 Answers 4

2

You can firstly interpret the bits into an array, using numpy.packbits(), then convert it to an array of bytes by applying bytearray(), then decode() it to be a normal string.

The following code

import numpy
np_bin_array = [0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 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]
print(bytearray(numpy.packbits(np_bin_array)).decode().strip("\x00"))

gives

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

3 Comments

I tried that but now it gives me this error: TypeError: only integer scalar arrays can be converted to a scalar index. I think this means that since the array is of bool type values it needs to be int type array? How to convert a bool array to an int array?
@pds360 can you give more information on how this np_bin_array is produced? I cannot reproduce the problem with a simple bool numpy array, I guess you are meaning something else?
Actually, I got around it and working with just using the np.packbits method and then converting each char code back to string and stop if it's 0. yy=[] yy_word="" yy=np.packbits(np_bin_array) for i in yy: if i: j = chr(i) yy_word += str(j) print(yy_word)
0

This one works to me. I had a boolean array though, so had to make an additional conversion.

def split_list(alist,max_size=1):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(alist), max_size):
        yield alist[i:i+max_size]


result = "".join([chr(i) for i in (int("".join([str(int(j)) for j in letter]), base=2) for letter in split_list(np_bin_array, 8)) if i != 0])

Comments

0
import numpy as np
np_bin_array = np.array([0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 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])
bhello = ''.join(map(str, np_bin_array))
xhello = hex(int(bhello, 2)).strip("0x")

''.join(chr(int(xhello[i:i+2], 16)) for i in range(0, len(xhello), 2))

Comments

0

I got it working with this:

np_bin_array = [0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 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]
yy=[]

yy_word=""

yy=np.packbits(np_bin_array)

for i in yy:
    if i:
        j = chr(i)

        yy_word += str(j)

print(yy_word)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.