我如何将数字转换为二进制表示?

时间:2013-11-05 20:22:18

标签: python python-3.x

如何将数字从二进制变为否定,并且代码错误:

print (" Enter a number")
realnum = int(input())
print("In Binary that is", bin(realnum)[2:])
input(" Press enter to finish ") 

2 个答案:

答案 0 :(得分:2)

print("In Binary that is", bin(realnum)[2:])
print("In Denary that is", realnum)

是你想要的......根据我如何解释你的问题

答案 1 :(得分:1)

看起来您正在尝试将输入转换为二进制。如果是这样,您可以使用format

>>> # Get the input as an integer
>>> realnum = int(input())
123
>>> # Make it binary
>>> format(realnum, '08b')
'01111011'
>>> x = format(realnum, '08b')
>>> # Put it back as an integer
>>> int(x, 2)
123
>>>

因此,您的代码变为:

print(" Enter a number")
realnum = int(input())
print("In Binary that is", format(realnum, '08b'))
print("In Denary that is", realnum)
input(" Press enter to finish ")