如何将输入二进制文件转换为文本python

时间:2018-01-05 07:46:26

标签: python python-3.x binary numbers decimal

我是Python编程的新手,我制作了一个关于将文本转换为二进制数的简单Python程序,现在我添加了一个简单的代码,用于将它们从二进制转换回文本(如果以下代码更正,请更正下面的代码有错误。)

message = (input("Enter Text/Binary to Translate: ")

binList = {
   '01100001': 'a', 
   '01100010': 'b', 
   # to Z 
} 

BinT = "".join([binList[message] for message in letter])

print(BinT)

2 个答案:

答案 0 :(得分:3)

您不应该使用查找字典。要将二进制字符串转换为ascii字符,请分别使用内置函数chrint

>>> chr(int('01100001', 2))
'a'

另一方面,分别使用内置函数formatord

>>> format(ord('a'), '08b')
'01100001'

答案 1 :(得分:0)

message = raw_input("Enter Text/Binary to Translate: ").split(' ')

binList = {
    '01100001': 'a',
    '01100010': 'b',
    # to Z
}

BinT = "".join( [binList[letter] for letter in message])

print(BinT)
相关问题