Turing101
Turing101

Reputation: 377

Code printing unwanted characters in python

I am trying to convert binary bit stream taken as string input to ascii. Here is the code for the same

# Python program to illustrate the
# conversion of Binary to ASCII

# Initializing a binary string in the form of
# 0 and 1, with base of 2
binary_int = int("11000010110001001100011", 2);

# Getting the byte number
byte_number = binary_int.bit_length() + 7 // 8

# Getting an array of bytes
binary_array = binary_int.to_bytes(byte_number, "big")

# Converting the array into ASCII text
ascii_text = binary_array.decode()

# Getting the ASCII value
print(ascii_text)

In (Python v3.6.2) it is giving output abc but in google colab I am getting this output ��������������������abc

This is how I have framed the logic. Firstly, call int(binary_sting, base) with the base as 2 indicating the binary string. and then call int.to_bytes(byte_number, byte_order) function, where byte_order is taken as “big” and byte_number is taken as the number of bytes that binary_int occupies to return an array of bytes. This byte_number can be found using the operation binary_int.bit_length() + 7 // 8. And then call array.decode operation to turn the array into ASCII text.

I am not able to understand why is it so. Can anyone help me with how to get the proper output in google colab. Or if someone can show me any other way to do the conversion it will be really helpful.

Upvotes: 3

Views: 217

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51683

Division is done before addition:

byte_number = binary_int.bit_length() + 7 // 8

is equal to

byte_number = binary_int.bit_length() + 0

See python operator precedence.

You are missing parentheses to order your math calculation correctly:

binary_int = int("11000010110001001100011", 2);

# calculate amount of bytes in the given number, rounded up to 
# full bytes if fewer then 8 full bits remain
byte_number = (binary_int.bit_length() + 7) // 8   # fixed

# Getting an array of bytes
binary_array = binary_int.to_bytes(byte_number, "big")

# Converting the array into ASCII text
ascii_text = binary_array.decode()

# Getting the ASCII value
print(ascii_text)

Outputs

abc

Upvotes: 3

Related Questions