-1

嗨,我的程序以这种方式打印:

消息转二进制:asd

01100001
01110011
01100100

但我需要像这样以水平形式打印消息(011000010111001101100100)

我一直在尝试我不知道该怎么做的一切。

def string_a_binario(s):

    for i in range (0, len(s)):

        if s[i] == " " : 
            print("00100001")
        elif s[i] == "!" :
            print("00100001")

        elif s[i] == "#" :
            print("00100011")
        elif s[i] == "$" :
            print("00100100")
        elif s[i] == "%" :
            print("00100101")
        elif s[i] == "&" :
            print("00100110")
        elif s[i] == "'" :
            print("00100111")
        elif s[i] == "(" :
            print("00101000")
        elif s[i] == ")" :
            print("00101001")
        elif s[i] == "*" :
            print("00101010")
        elif s[i] == "+" :
            print("00101011")
        elif s[i] == "," :
            print("00101100")
        elif s[i] == "-" :
            print("00101101")
        elif s[i] == "." :
            print("00101110")
        elif s[i] == "/" :
            print("00101111")
        elif s[i] == "0" :
            print("00110000")
        elif s[i] == "1" :
            print("00110001")
        elif s[i] == "2" :
            print("00110010")
        elif s[i] == "3" :
            print("00110011")
        elif s[i] == "4" :
            print("00110100")
        elif s[i] == "5" :
            print("00110101")
        elif s[i] == "6" :
            print("00110110")
        elif s[i] == "7" :
            print("00110111")
        elif s[i] == "8" :
            print("00111000")
        elif s[i] == "9" :
            print("00111001")
        elif s[i] == ":" :
            print("00111010")
        elif s[i] == ";" :
            print("00111011")
        elif s[i] == "<" :
            print("00111100")
        elif s[i] == "=" :
            print("00111101")
        elif s[i] == ">" :
            print("00111110")
        elif s[i] == "?" :
            print("00111111")
        elif s[i] == "@" :
            print("01000000")
        elif s[i] == "A" :

            print("01000001")
        elif s[i] == "B" :
            print("01000010")

        elif s[i] == "C" :
            print("01000011")

        elif s[i] == "D" :
            print("01000100")
        elif s[i] == "E" :
            print("01000101")
        elif s[i] == "F" :
            print("01000110")
        elif s[i] == "G" :
            print("01000111")
        elif s[i] == "H" :
            print("01001000")
        elif s[i] == "I" :
            print("01001001")
        elif s[i] == "J" :
            print("01001010")
        elif s[i] == "K" :
            print("01001011")
        elif s[i] == "L" :
            print("01001100")
        elif s[i] == "M" :
            print("01001101")
        elif s[i] == "Ñ" :
            print("01001110")
        elif s[i] == "O" :
            print("01001111")
        elif s[i] == "P" :
            print("01010000")
        elif s[i] == "Q" :
            print("01010001")
        elif s[i] == "R" :
            print("01010010")
        elif s[i] == "O" :
            print("01001111")
        elif s[i] == "P" :
            print("01010000")
        elif s[i] == "Q" :
            print("01010001")
        elif s[i] == "R" :
            print("01010010")
        elif s[i] == "S" :
            print("01010011")

        elif s[i] == "T" :
            print("01010100")
        elif s[i] == "U" :
            print("01010101")
        elif s[i] == "V" :
            print("01010110")
        elif s[i] == "W" :
            print("01010111")
        elif s[i] == "X" :
            print("01011000")
        elif s[i] == "Y" :
            print("01011001")
        elif s[i] == "Z" :
            print("01011010")

        elif s[i] == "[" :
            print("01011011")
        elif s[i] == " \ "  :
            print("01011100")
        elif s[i] == "]" :
            print("01011101")
        elif s[i] == "^" :
            print("01011110")
        elif s[i] == "_" :
            print("01011111")
        elif s[i] == "`" :
            print("01100000")


        elif s[i] == "a" :
            print("01100001")
        elif s[i] == "b" :
            print("01100010")
        elif s[i] == "c" :
            print("01100011")
        elif s[i] == "d" :
            print("01100100")
        elif s[i] == "e" :
            print("01100101")
        elif s[i] == "f" :
            print("01100110")
        elif s[i] == "g" :
            print("01100111")
        elif s[i] == "h" :
            print("01101000")
        elif s[i] == "i" :
            print("01101001")
        elif s[i] == "j" :
            print("01101010")
        elif s[i] == "k" :
            print("01101011")
        elif s[i] == "l" :
            print("01101100")
        elif s[i] == "m" :
            print("01101101")
        elif s[i] == "n" :
            print("01101110")
        elif s[i] == "o" :
            print("01101111")
        elif s[i] == "p" :
            print("01110000")
        elif s[i] == "q" :
            print("01110001")
        elif s[i] == "r" :
            print("01110010")
        elif s[i] == "s" :
            print("01110011")
        elif s[i] == "t" :
            print("01110100")
        elif s[i] == "u" :
            print("01110101")
        elif s[i] == "v" :
            print("01110110")
        elif s[i] == "w" :
            print("01110111")
        elif s[i] == "x" :
            print("01111000")
        elif s[i] == "y" :
            print("01111001")
        elif s[i] == "z" :
            print("01111010")
        elif s[i] == "{" :
            print("01111011")
        elif s[i] == "|" :
            print("01111100")
        elif s[i] == "}" :
            print("01111101")
        elif s[i] == "~" :
            print("01111110")

s = list(input("Message to binary:"))
string_a_binario(s)
4

3 回答 3

3

至于多行问题,如果你在语句,末尾加上逗号()print,它不会打印换行符:

例子:

print 'Put these on',
print 'just one line',
print 'please'

印刷:

Put these on just one line please

注意:这仍然会在每个项目之后打印空格。如果这是不可接受的,请sys.stdout.write()Mai 显示的那样使用。


有一种对每个案例都进行硬编码更好的方法来实现这一点!(我无法想象你写了多长时间!)

提示:

其他python提示:

  • 无需将结果转换为input- listpython 中的字符串是可迭代的。

  • 这不是 C - 您不需要使用数组索引进行迭代。相反,您可以遍历序列中的每个项目。在这里,这些项目是字符串的每个字符!

for i in range (0, len(s)):
    if s[i] == " " :
       ...

可以换成

for x in s:
    is x == " ":
        ...
于 2013-10-19T19:40:46.697 回答
1

假设我有两个字符串s1s2

import sys
sys.stdout.write(s1)
sys.stdout.write(s2)
sys.stdout.write('\n')
sys.stdout.flush()

然后 s1 和 s2 将打印在同一行。

希望能帮助到你:)

于 2013-10-19T19:39:51.910 回答
0

我只是想说明你是如何编写函数的,它可能会引入一些错误。例如,查看if循环中的前两个语句,看起来空格和!字符都是 output 00100001,这是需要的吗?这是与您的功能类似的东西,并且可能会消除一些人为错误。

def string_a_binario(s):
    for i in range(len(s)):
        sys.stdout.write("{0:08b}".format(ord(s[i])))

此代码还可以根据需要水平写入。但是,它对所有输入的功能与您的功能不同。例如,如果不满足任何条件,您的代码不会打印任何内容,而此函数可以打印您无法打印的内容。

在 python 3(也许是以前的版本,我不确定)中,您可以告诉 print 不要包含换行符,这是该函数的另一个版本:

def string_a_binario(s):
    for i in range(len(s)):
        print("{0:08b}".format(ord(s[i])), end='')

这是 python 3 中的另一个版本,它在每个二进制表示中都放置了一个括号:

def string_a_binario(s):
    for i in range(len(s)):
        print("[{0:08b}".format(ord(s[i])), end=']')
于 2013-10-19T20:12:12.650 回答