[英]convert dec number to 6 bit binary number
I am looking to convert a dec number to a 6 bit binary number.. bin()
works fine but omits leading zeros which are important. 我期待将dec数转换为6位二进制数
bin()
工作正常,但省略了重要的前导零。
for example: 例如:
etc... with the largest dec number allowed being 63. 等...允许的最大分数为63。
Either what Matt said in the comment ( bin(63)[2:].zfill(6)
), or use format strings in Python 2.6+: 要么Matt在评论中说(
bin(63)[2:].zfill(6)
),要么在Python 2.6+中使用格式字符串 :
'{0:06b}'.format(63)
You can omit the first zero in Python 2.7+ as you can implicitly number groups. 您可以省略Python 2.7+中的第一个零,因为您可以隐式编号组。
Or just: 要不就:
n2=[2**x for x in xrange(0, 7)]
n2.reverse()
def getx(x):
ret = ''
for z in n2:
if x >= z:
x -= z
ret += '1'
else:
ret += '0'
return ret
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.