Convert binary to ASCII and vice versa

前端 未结 8 1433
余生分开走
余生分开走 2020-11-22 03:22

Using this code to take a string and convert it to binary:

bin(reduce(lambda x, y: 256*x+y, (ord(c) for c in \'hello\'), 0))

this outputs:<

8条回答
  •  面向向阳花
    2020-11-22 04:27

    This is my way to solve your task:

    str = "0b110100001100101011011000110110001101111"
    str = "0" + str[2:]
    message = ""
    while str != "":
        i = chr(int(str[:8], 2))
        message = message + i
        str = str[8:]
    print message
    

提交回复
热议问题