Decode it.

>>> b'a string'.decode('ascii')
'a string'

To get bytes from string, encode it.

>>> 'a string'.encode('ascii')
b'a string'
Answer from falsetru on Stack Overflow
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ built-in-methods โ€บ convert-binary-string-to-normal-string
4 ways to Convert a Binary String to a Normal String - AskPython
April 23, 2023 - A variable called ascs is used to store the conversion of the binary data to normal string with the help of tobytes and decode.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ convert-binary-to-string-using-python
Convert binary to string using Python - GeeksforGeeks
April 12, 2025 - This method first converts the binary into a hexadecimal string, then decodes it using the codecs module.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ binary-to-utf8-conversion
How to Convert Binary Data to UTF-8 in Python - AskPython
April 10, 2025 - Another technique that works for unknown binary data is to base64 encode it first. Base64 produces UTF-8 compatible text output from any binary input. import base64 data = b"\x12\xab" # Base64 encode b64_data = base64.b64encode(data) # Now decode base64 text from UTF-8 to string text = b64_data.decode("utf-8") print(text) # Output: Eqs=
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-bytes-to-string-how-to-convert-a-bytestring
Python Bytes to String โ€“ How to Convert a Bytestring
April 10, 2023 - This is useful when we are working with binary data, or when converting between different types of data that use bytes as their underlying representation. ... # Define a string string = "Hello, world!" # Convert the string to a bytes object bytes_object = bytes(string, 'utf-8') # Print the bytes object print(bytes_object) # Convert the bytes object back to a string decoded_string = bytes_object.decode('utf-8') # Print the decoded string print(decoded_string)
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ binascii.html
binascii โ€” Convert between binary and ASCII
Changed in version 3.3: ASCII-only unicode strings are now accepted by the a2b_* functions. The binascii module defines the following functions: ... Convert a single line of uuencoded data back to binary and return the binary data.
๐ŸŒ
Real Python
realpython.com โ€บ convert-python-bytes-to-strings
How to Convert Bytes to Strings in Python โ€“ Real Python
July 18, 2026 - Converting bytes into readable strings in Python is an effective way to work with raw bytes fetched from files, databases, or APIs. You can do this in just three steps using the bytes.decode() method.
Find elsewhere
Top answer
1 of 3
3

You are looping over the individual characters of the input message, but you need to instead look for groups of 9 characters (2 times 4 binary digits and the space). Your mapping has keys like '0100 1001', not '0' and '1' and ' '

The simplest approach (albeit a bit brittle) would be to loop over indices in steps of 10 characters (1 extra for the space between the characters), then grab 9 characters:

for i in xrange(0, len(messageDecode), 10):
    group = messageDecode[i:i + 9]
    print inverseBINARY[group],    

The xrange() object produces integers 10 apart; so 0, 10, 20, etc. The messageDecode string is then sliced to grab 9 characters starting at that index, so messageDecode[0:9] and messageDecode[10:19], messageDecode[20:29], etc.

A more robust approach would be to remove all spaces and grab blocks every 8 characters; that'd leave room for extra spaces in between, but you do have to re-insert that space to match your keys:

messageDecode = messageDecode.replace(' ', '')
for i in xrange(0, len(messageDecode), 8):
    group = messageDecode[i:i + 4] + ' ' + messageDecode[i + 4:i + 8]
    print inverseBINARY[group],    

or you could perhaps not include the spaces in your inverseBINARY mapping here:

inverseBINARY = {v.replace(' ', ''): k for k, v in BINARY.items()}

and then simply slice every 8 characters:

messageDecode = messageDecode.replace(' ', '')
for i in xrange(0, len(messageDecode), 8):
    group = messageDecode[i:i + 8]
    print inverseBINARY[group], 
2 of 3
-1

if you want to decode binary why not use native functions as the binary number and chr ?

>>> print chr(0b01000010)
B

EDIT

Ok then, this is how i would solve that:

from string import letters, punctuation
encode_data = {letter:bin(ord(letter)) for letter in letters+punctuation+' '}
decode_data = {bin(ord(letter)):letter for letter in letters+punctuation+' '}

def encode(message):
    return [encode_data[letter] for letter in message]

def decode(table):
    return [decode_data[item] for item in table]

encoded = encode('hello there')
print decode(encoded) # ['h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e']
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 75640460 โ€บ python-encoding-decoding-with-binary-strings
git - Python Encoding/Decoding with Binary Strings - Stack Overflow
The file YOU are opening is a gzip archive. NOTHING you read from that is human-readable. You are passing that file to the zlib decompressor. That library returns byte strings to you, because the contents COULD be other binary files, but if the contents are text, you can decode them to Unicode strings.
๐ŸŒ
IT trip
en.ittrip.xyz โ€บ python
The Essential Guide to Encoding and Decoding Binary Files in Python | IT trip
November 4, 2023 - Sometimes, you may want to convert binary data to a hexadecimal string for display or other purposes. Pythonโ€™s `binascii` module is perfect for such operations. import binascii hex_str = binascii.hexlify(binary_data) Encoding and decoding binary files in Python is a versatile skill thatโ€™s applicable in many programming scenarios, from data storage to network communication.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - Base64 encoding is a popular technique ... cannot process raw binary data but would readily handle text. With Python, we can use the base64 module to Base64 encode and decode text and binary data....
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python binary string to ascii string โ€“ and vice versa
Python Binary String to ASCII String - and Vice Versa - Be on the Right Side of Change
December 5, 2022 - To convert the binary string to an (ASCII) string, use the Integer to_bytes() method after converting the binary string to a normal integer using int() with the base=2 argument. def bin_to_str(x): ''' Converts a Binary String to an (ASCII) string''' ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Easily Decode Binary Numbers in Python - YouTube
In this video I show you how easy it is to convert binary numbers to base 10 in Python.#shorts #python
Published: September 20, 2021
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 76733560 โ€บ decoding-binary-data-in-python
Decoding Binary Data in Python - Stack Overflow
In the function that gets the bits, I divide the position by 8 to get the byte number where the string starts. Since a field might span several bytes, I took the easy way out and convert the 4 bytes starting at that byte number to a 32-bit integer.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 57308160 โ€บ decoding-string-list-in-python-from-a-binary-file
Decoding String list in python from a binary file - Stack Overflow
Now my string is: b'-\x00deg/s^2\x00m/s^2\x00m/s^2\x00rpm\x00m/s\xb2\x00m/s\xb2\x00m/s\xb2\x00m/s\xb2\x00m/s\xb2\x00m/s\xb2\x00deg' When I check the ord('ยฒ') it is 178, for your ref. Please help me with this. ... The error message that I see is, UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 595: invalid start byte ... I have fixed decode error i assume you don't want to get the decode value of ' 0xb2' i.e my_binary_out.decode('utf-8', 'ignore').split('\x00') which should give you your desired values ['-', 'deg/s^2', 'm/s^2', 'm/s^2', 'rpm', 'm/s', 'm/s', 'm/s', 'm/s', 'm/s', 'm/s', 'deg']
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-bytesdecode-in-python
What is bytes.decode() in Python?
bytes.decode() is used to decode bytes to a string object. Decoding to a string object depends on the specified arguments. It also allows us to mention an error handling scheme to use for seconding errors.
๐ŸŒ
Medium
medium.com โ€บ @garybao โ€บ how-to-convert-bytes-to-strings-in-python-using-the-decode-function-c81d5b1ee09e
How to Convert Bytes to Strings in Python Using the decode() Function | by Gary Bao | Medium
June 20, 2025 - It transforms a sequence of bytes into a string (str) using a specified character encoding, such as 'utf-8' or 'ascii'. In simpler terms, it translates machine-readable binary data into human-readable text.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ working-with-binary-data-in-python
Working with Binary Data in Python - GeeksforGeeks
June 22, 2020 - As seen above it is possible to encode or decode strings and binary strings using the encode() or decode() function. We need the encoding because in some encodings it is not possible to to decode the strings.