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!

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
pds360
  • 31
  • 1
  • 2

4 Answers4

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
Chazeon
  • 546
  • 2
  • 14
  • 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 Feb 18 '17 at 01:54
  • @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? – Chazeon Feb 18 '17 at 02:47
  • 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) – pds360 Feb 18 '17 at 03:47
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])
Highstaker
  • 1,015
  • 2
  • 12
  • 28
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))
Whitequill Riclo
  • 788
  • 2
  • 8
  • 19
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)
Highstaker
  • 1,015
  • 2
  • 12
  • 28
pds360
  • 31
  • 1
  • 2