将十进制列表转换为二进制python

时间:2018-01-25 01:38:50

标签: python-3.x

所以我有一个我正在处理的代码。我遇到了一个问题,我无法弄清楚将十进制列表转换为二进制的方法。我搜索过示例,但无法找到解决方案。如果你能帮我解决这个问题,那将非常感谢。

代码

PT = input("Please enter your plaintext: ")
PT = PT.lower()
K = input("Please enter your key: ")
K = K.lower()
output1 = []
output2 = []
for character in PT:
    if character.isdigit():
        digit = ord(character) - 22
        output1.append(digit)
    elif character.isalpha():
        number = ord(character) - 97
        output1.append(number)
    else:
        print("Please use letters and numbers only. (Plaintext)")
        break

for character in K:
    if character.isdigit():
        digit = ord(character) - 22
        output2.append(digit)
    elif character.isalpha():
        number = ord(character) - 97
        output2.append(number)
    else:
        print("Please use letters and numbers only. (Key)")
        break

1 个答案:

答案 0 :(得分:0)

您可以使用bin()将对象转换为二进制文件。

PT = raw_input("Please enter your plaintext: ")
PT = PT.lower()
K = raw_input("Please enter your key: ")
output1 = []
output2 = []
for character in PT:
    if character.isdigit():
        digit = ord(character) - 22
        output1.append(bin(digit))
    elif character.isalpha():
        number = ord(character) - 97
        output1.append(bin(number))
    else:
        print("Please use letters and numbers only. (Plaintext)")
        break
for character in K:
    if character.isdigit():
        digit = ord(character) - 22
        output2.append(bin(digit))
    elif character.isalpha():
        number = ord(character) - 97
        output2.append(bin(number))
    else:
        print("Please use letters and numbers only. (Key)")
        break
print output1
print output2

测试案例

请输入您的明文:abc

请输入您的密钥:234

[' 0b0',' 0b1',' 0b10']

[' 0b11100',' 0b11101',' 0b11110']