我是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)
答案 0 :(得分:3)
您不应该使用查找字典。要将二进制字符串转换为ascii字符,请分别使用内置函数chr
和int
:
>>> chr(int('01100001', 2))
'a'
>>> 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)