下面是python的表达式。它将整数转换为二进制。
>>>octtab = {'0':'000', '1':'001', '2':'010', '3':'011',
'4':'100', '5':'101', '6':'110', '7':'111'}
>>>def bin1(d, width=0):
"integer to binary (string)"
s = "%o" % d
b = ''
for el in s:
b += octtab[el]
if width > 0:
if len(s) > width:
return b[:width]
b = b.zfill(width)
return b我不知道%o的意思。提前谢谢:)
发布于 2013-07-03 10:59:29
%o是string formatting。将%o用于八进制数(即基数为8的数):
>>> print "%o" % 011
11
>>> print "%o" % 8
10 # Because 010 == 8https://stackoverflow.com/questions/17438952
复制相似问题