所以我有这个程序,它给出了ascii值的二进制值加上一个用户输入字符串的1。

text = input()
bitString = ''
for ch in text:
    new = ord(ch) + 1

    decimal = new
    while decimal > 0:
        remainder = decimal % 2
        decimal = decimal // 2
        bitString = str(remainder) + bitString

print(bitString)

如果用户输入“abcde”,则输出为
11001101100101110010011000111100010

我如何获取二进制值以空格分隔,输出将是
1100010 1100011 1100100 1100101 1100110

最佳答案

在代码中进行很小的改动以增加空间:

text = input()
bitString = ''
for ch in text:
    new = ord(ch) + 1

    decimal = new
    while decimal > 0:
        remainder = decimal % 2
        decimal = decimal // 2
        bitString = str(remainder) + bitString

    bitString = " " + bitString

print(bitString)

输出:
1100010 1100011 1100100 1100101 1100110

10-06 08:43