0

I want to convert a binary string into ASCII. The string contains 588 token {0, 1}. I've tried it with binascii: https://docs.python.org/3/library/binascii.html Or like this: Convert binary to ASCII and vice versa

Sample-Code:

sentence ="0b0101010101..."
n = int(sentence, 2)
print (n.to_bytes((n.bit_length() + 7) // 8, 'big').decode())

Output:

Error: print (n.to_bytes((n.bit_length() + 7) // 8, 'big').decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x87 in position 1: invalid start byte

Edit: this is now my solution any ideas to optimize the code?

 sentence ="0b0101010101..."
 length=(int(len(sentence)/8)*8)+2
 n = int(sentence[:length:1], 2)
 print (n.to_bytes((n.bit_length() + 7) // 8, 'big').decode("utf_8", "ignore"))

SOLVED!

Community
  • 1
  • 1
the_new_one
  • 33
  • 1
  • 5
  • You don't have ASCII input if you have a 0x87 byte in there. Are you 100% certain the value can be divvied up into bytes every 8 bits? – Martijn Pieters Apr 03 '15 at 18:36
  • https://docs.python.org/2/library/binascii.html – Robert Harvey Apr 03 '15 at 18:36
  • Can you provide us with the actual value here? – Martijn Pieters Apr 03 '15 at 18:36
  • @RobertHarvey: Python 3, and they have a *binary* value in a string. The `binascii` module is not going to help there. – Martijn Pieters Apr 03 '15 at 18:37
  • Is the binary value Unicode, and he wants it in ASCII? What the hell? – Robert Harvey Apr 03 '15 at 18:37
  • 588 bits is *not a multiple of 8*. You have 4 bits too many or are 4 bits short. It is a multiple of 7 though, and ASCII only requires 7 bits... – Martijn Pieters Apr 03 '15 at 18:38
  • Okay, this method only works if the string is divisible by 8 and if it contains only "utf-8" signs. Code works: n = int(sentence[:578:1], 2) Is there an option to ignore other signs if they are not in "utf-8" and to cut the string automatically. I tried a only converter there it worked fine with no changes on the string(len=588, signs which are not in "utf-8": http://www.binaryhexconverter.com/binary-to-ascii-text-converter – the_new_one Apr 03 '15 at 18:53
  • @the_new_one: So what is contained in those last 10 bits? Where does the value come from? You can tell `bytes.decode()` to use a different error handler, sure, but perhaps you can avoid those bits in the first place. – Martijn Pieters Apr 03 '15 at 20:46
  • The last 10 bits are wrong values: "111111111111" the ASCII values for this is "�" and python can't handle with that. Is there a chance to put the whole string as input and to avoid the error message? If I don't know if the binary is a correct ASCII value, I will always get an error and it doesn't care if there are correct ASCII values in it. Look at the start post @Edit. – the_new_one Apr 04 '15 at 11:49

0 Answers0