Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code printing unwanted characters in python

Tags:

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.

like image 443
Turing101 Avatar asked Mar 07 '26 07:03

Turing101


1 Answers

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
like image 160
Patrick Artner Avatar answered Mar 08 '26 20:03

Patrick Artner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!