How Do I Decode This Binary String in Python?

How Do I Decode This Binary String in Python?

So, I have this string 01010011101100000110010101101100011011000110111101110100011010000110010101110010011001010110100001101111011101110111100101101111011101010110010001101111011010010110111001100111011010010110110101100110011010010110111001100101011000010111001001100101011110010110111101110101011001100110100101101110011001010101000000000000

and I want to decode it using python, I'm getting this error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 280: invalid start byte

According to this webiste:

The output should be S�ellotherehowyoudoingimfineareyoufineP

Here's my code:

def decodeAscii(bin_string):
    binary_int = int(bin_string, 2);
  
    byte_number = binary_int.bit_length() + 7 // 8
    binary_array = binary_int.to_bytes(byte_number, "big")
    ascii_text = binary_array.decode()
    
    print(ascii_text)

How do I fix it?

2

3 Answers

Your bytes simply cannot be decoded as utf-8, just as the error message tells you.

utf-8 is the default encoding parameter of decode - and the best way to put in the correct encoding value is to know the encoding - otherwise you'll have to guess.

And guessing is probably what the website does, too, by trying the most common encodings, until one does not throw an exception:

def decodeAscii(bin_string):
    binary_int = int(bin_string, 2);
    byte_number = binary_int.bit_length() + 7 // 8
    binary_array = binary_int.to_bytes(byte_number, "big")
    ascii_text = "Bin string cannot be decoded"
    for enc in ['utf-8', 'ascii', 'ansi']:
        try:
            ascii_text = binary_array.decode(encoding=enc)
            break
        except:
            pass
    print(ascii_text)

s = "01010011101100000110010101101100011011000110111101110100011010000110010101110010011001010110100001101111011101110111100101101111011101010110010001101111011010010110111001100111011010010110110101100110011010010110111001100101011000010111001001100101011110010110111101110101011001100110100101101110011001010101000000000000"
decodeAscii(s)

Output:

S°ellotherehowyoudoingimfineareyoufineP

But there's no guarantee that you find the "correct" encoding by guessing.

0

Your binary string is just not a valid ascii or utf-8 string. You can tell decode to ignore invalid sequences by saying

ascii_text = binary_array.decode(errors='ignore')
1

It could be solved in one line:

Try this:

def bin_to_text(bin_str):
    bin_to_str = "".join([chr(int(bin_str[i:i+8],2)) for i in range(0,len(bin_str),8)])

    return bin_to_str

bin_str = '01010011101100000110010101101100011011000110111101110100011010000110010101110010011001010110100001101111011101110111100101101111011101010110010001101111011010010110111001100111011010010110110101100110011010010110111001100101011000010111001001100101011110010110111101110101011001100110100101101110011001010101000000000000'
bin_to_str = bin_to_text(bin_str)
print(bin_to_str)

Output:

S°ellotherehowyoudoingimfineareyoufineP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.