我正在try 将作为字符串输入的二进制位流转换为ascii.下面是同样的代码
# 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)
在(Python v3.6.2)中,它给出了abc
个输出,但在google colab中,我得到了��������������������abc
个输出
这就是我构建逻辑的方式.首先,调用int(binary_sting,base),其中base为2表示二进制字符串.然后调用int.to_bytes(byte_number,byte_order)函数,其中byte_order表示"大",byte_number表示二进制_int返回字节数组所占用的字节数.这个字节号可以通过二进制_int.bit_length()+7//8操作找到.然后调用array.解码操作将数组转换为ASCII文本.
我不明白为什么会这样.有谁能帮我在google colab中获得正确的输出吗.或者,如果有人能告诉我任何其他方式来做转换,这将是非常有帮助的.