0

I have a string which is like that "00100101 10010011 01010100". Every 8 bits there is a space and the string might be bigger. I want to convert it to plaintext with python3.

I am new in python and I tried some solutions I found here but without success.

2 Answers2

0
string = "00100101 10010011 01010100"
string_list = string.split()

def bin2str(s):
    return ''.join([chr(int(s[i:i+8], 2)) for i in range(0, len(s), 8)])

for s in string_list:
    print(bin2str(s))

Would solve your problem and print '%', '\x93' and 'T'

cnemri
  • 454
  • 4
  • 14
0

I think this should work

bits = "00100101 10010011 01010100"
#split the string into a list of strings of 8 bits 
bits_list = bits.split(" ")
#convert it with chr(int(x,2)) and print to screen
"".join([chr(int(x,2)) for x in bits_list])

or to print

bits = "00100101 10010011 01010100"
#split the string into a list of strings of 8 bits 
bits_list = bits.split(" ")
#convert it with chr(int(x,2)) and join into a new string
_ = [print(chr(int(x,2))) for x in bits_list]
Lewis Morris
  • 1,916
  • 2
  • 28
  • 39