IFRAME SYNC
def binary_to_text(binary_str):
try:
# Split binary string into 8-bit chunks
chunks = [binary_str[i:i+8] for i in range(0, len(binary_str), 8)]
# Convert each 8-bit chunk to decimal and then to ASCII
text_data = ''.join([chr(int(chunk, 2)) for chunk in chunks])
return text_data
except ValueError:
print("Error: Invalid binary string")
# Example usage
binary_string = "01001000 01100101 01101100 01101100 01101111" # Example binary string
binary_string = ''.join(binary_string.split()) # Remove spaces from binary string
text_result = binary_to_text(binary_string)
if text_result:
print("Text result:", text_result)def binary_to_text(binary_str):
try:
# Remove any spaces or non-binary characters
binary_str = ''.join(filter(lambda x: x in '01', binary_str))
# Ensure the binary string has a length that is a multiple of 8
padding = len(binary_str) % 8
if padding != 0:
binary_str = binary_str[:-padding]
# Split binary string into 8-bit chunks
chunks = [binary_str[i:i+8] for i in range(0, len(binary_str), 8)]
# Convert each 8-bit chunk to decimal and then to ASCII
text_data = ''.join([chr(int(chunk, 2)) for chunk in chunks])
return text_data
except ValueError:
print("Error: Invalid binary string")
IFRAME SYNC
# Example usage
binary_data = "01001000 01100101 01101100 01101100 01101111" # Example binary data with spaces
text_result = binary_to_text(binary_data)
if text_result:
print("Text result:", text_result)
Comments
Post a Comment