I'm trying to convert an integer to binary using the bin() function in Python. However, it always removes the leading zeros, which I actually need, such that the result is always 8-bit:
我正在尝试使用Python中的bin()函数将整数转换为二进制。但是,它总是删除我实际需要的前导零,这样结果总是8位:
Example:
例:
bin(1) -> 0b1
# What I would like:
bin(1) -> 0b00000001
Is there a way of doing this?
有办法做到这一点吗?
8 个解决方案
#1
125
Use the format()
function:
使用format()函数:
>>> format(14, '#010b')
'0b00001110'
The format()
function simply formats the input following the Format Specification mini language. The #
makes the format include the 0b
prefix, and the 010
size formats the output to fit in 10 characters width, with 0
padding; 2 characters for the 0b
prefix, the other 8 for the binary digits.
format()函数只是按照格式规范迷你语言格式化输入。 #使格式包含0b前缀,010大小格式化输出以适合10个字符宽度,0填充; 0b前缀为2个字符,二进制数字为8个字符。
This is the most compact and direct option.
这是最紧凑和直接的选择。
If you are putting the result in a larger string, use str.format()
and put the second argument for the format()
function after the colon of the placeholder {:..}
:
如果要将结果放在一个更大的字符串中,请使用str.format()并在占位符{:..}的冒号后面放置format()函数的第二个参数:
>>> 'The produced output, in binary, is: {:#010b}'.format(14)
'The produced output, in binary, is: 0b00001110'
If you did not want the 0b
prefix, simply drop the #
and adjust the length of the field:
如果你不想要0b前缀,只需删除#并调整字段的长度:
>>> format(14, '08b')
'00001110'
#2
83
>>> '{:08b}'.format(1)
'00000001'
See: Format Specification Mini-Language
请参阅:格式规范迷你语言
Note for Python 2.6 or older, you cannot omit the positional argument identifier before :
, so use
对于Python 2.6或更早版本的注意事项,在以下情况之前不能省略位置参数标识符,因此请使用
>>> '{0:08b}'.format(1)
'00000001'
#3
11
I am using
我在用
bin(1)[2:].zfill(8)
will print
将打印
'00000001'
#4
6
You can use the string formatting mini language:
您可以使用字符串格式化迷你语言:
def binary(num, pre='0b', length=8, spacer=0):
return '{0}{{:{1}>{2}}}'.format(pre, spacer, length).format(bin(num)[2:])
Demo:
演示:
print binary(1)
Output:
输出:
'0b00000001'
EDIT: based on @Martijn Pieters idea
编辑:基于@Martijn Pieters的想法
def binary(num, length=8):
return format(num, '#0{}b'.format(length + 2))
#5
1
Sometimes you just want a simple one liner:
有时你只想要一个简单的衬垫:
binary = ''.join(['{0:08b}'.format(ord(x)) for x in input])
Python 3
Python 3
#6
0
You can use something like this
你可以使用这样的东西
("{:0%db}"%length).format(num)
#7
0
module Adder(
input upperBit, lowerBit, c_in,
output s, c_out)
write gate1, gate2, gate3
xor (gate1, upperBit, lowerBit)
xor (s, gate1, c_in)
and (upperBit, lowerBit)
and (gate1, c_in)
or (c_out, gate1, gate2)
endmodule
module ful_adder8(
input [7:0) a, b
input c_in
output [7:0) s,
output c_out)
write [7:0] carry
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
endmodule
test
def split (n):
return (n&0x1,n&0x2,n&0x4,n&0x8,n&0x10,n&0x20,n&0x40,n&0x80)
def glue (b0,b1,b2,b3,b4,b5,b6,b7,c):
t = 0
if b0:
t += 1
if b1:
t += 2
if b2:
t += 4
if b3:
t += 8
if b4:
t += 16
if b5:
t += 32
if b6:
t += 64
if b7:
t += 128
if c:
t += 256
return t
def myadd (a,b):
(a0,a1,a2,a3,a4,a5,a6,a7) = split(a)
(b0,b1,b2,b3,b4,b5,b6,b7) = split(b)
(s0,s1,s2,s3,s4,s5,s6,s7,c) = addEightBits(a0,a1,a2,a3,a4,a5,a6,a7,b0,b1,b2,b3,b4,b5,b6,b7,false)
return glue (s0,s1,s2,s3,s4,s5,s6,s7,c)
#8
-1
You can use zfill:
你可以使用zfill:
print str(1).zfill(2)
print str(10).zfill(2)
print str(100).zfill(2)
prints:
打印:
01
10
100
I like this solution, as it helps not only when outputting the number, but when you need to assign it to a variable... e.g. - x = str(datetime.date.today().month).zfill(2) will return x as '02' for the month of feb.
我喜欢这个解决方案,因为它不仅有助于输出数字,还有助于将其分配给变量......例如 - x = str(datetime.date.today()。month).zfill(2)将为feb月份返回x为'02'。
#1
125
Use the format()
function:
使用format()函数:
>>> format(14, '#010b')
'0b00001110'
The format()
function simply formats the input following the Format Specification mini language. The #
makes the format include the 0b
prefix, and the 010
size formats the output to fit in 10 characters width, with 0
padding; 2 characters for the 0b
prefix, the other 8 for the binary digits.
format()函数只是按照格式规范迷你语言格式化输入。 #使格式包含0b前缀,010大小格式化输出以适合10个字符宽度,0填充; 0b前缀为2个字符,二进制数字为8个字符。
This is the most compact and direct option.
这是最紧凑和直接的选择。
If you are putting the result in a larger string, use str.format()
and put the second argument for the format()
function after the colon of the placeholder {:..}
:
如果要将结果放在一个更大的字符串中,请使用str.format()并在占位符{:..}的冒号后面放置format()函数的第二个参数:
>>> 'The produced output, in binary, is: {:#010b}'.format(14)
'The produced output, in binary, is: 0b00001110'
If you did not want the 0b
prefix, simply drop the #
and adjust the length of the field:
如果你不想要0b前缀,只需删除#并调整字段的长度:
>>> format(14, '08b')
'00001110'
#2
83
>>> '{:08b}'.format(1)
'00000001'
See: Format Specification Mini-Language
请参阅:格式规范迷你语言
Note for Python 2.6 or older, you cannot omit the positional argument identifier before :
, so use
对于Python 2.6或更早版本的注意事项,在以下情况之前不能省略位置参数标识符,因此请使用
>>> '{0:08b}'.format(1)
'00000001'
#3
11
I am using
我在用
bin(1)[2:].zfill(8)
will print
将打印
'00000001'
#4
6
You can use the string formatting mini language:
您可以使用字符串格式化迷你语言:
def binary(num, pre='0b', length=8, spacer=0):
return '{0}{{:{1}>{2}}}'.format(pre, spacer, length).format(bin(num)[2:])
Demo:
演示:
print binary(1)
Output:
输出:
'0b00000001'
EDIT: based on @Martijn Pieters idea
编辑:基于@Martijn Pieters的想法
def binary(num, length=8):
return format(num, '#0{}b'.format(length + 2))
#5
1
Sometimes you just want a simple one liner:
有时你只想要一个简单的衬垫:
binary = ''.join(['{0:08b}'.format(ord(x)) for x in input])
Python 3
Python 3
#6
0
You can use something like this
你可以使用这样的东西
("{:0%db}"%length).format(num)
#7
0
module Adder(
input upperBit, lowerBit, c_in,
output s, c_out)
write gate1, gate2, gate3
xor (gate1, upperBit, lowerBit)
xor (s, gate1, c_in)
and (upperBit, lowerBit)
and (gate1, c_in)
or (c_out, gate1, gate2)
endmodule
module ful_adder8(
input [7:0) a, b
input c_in
output [7:0) s,
output c_out)
write [7:0] carry
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
full_adder fa0(
a(a[o])
b(b[0])
c_in(c_in)
s(s[0])
c_out(carry[0]))
endmodule
test
def split (n):
return (n&0x1,n&0x2,n&0x4,n&0x8,n&0x10,n&0x20,n&0x40,n&0x80)
def glue (b0,b1,b2,b3,b4,b5,b6,b7,c):
t = 0
if b0:
t += 1
if b1:
t += 2
if b2:
t += 4
if b3:
t += 8
if b4:
t += 16
if b5:
t += 32
if b6:
t += 64
if b7:
t += 128
if c:
t += 256
return t
def myadd (a,b):
(a0,a1,a2,a3,a4,a5,a6,a7) = split(a)
(b0,b1,b2,b3,b4,b5,b6,b7) = split(b)
(s0,s1,s2,s3,s4,s5,s6,s7,c) = addEightBits(a0,a1,a2,a3,a4,a5,a6,a7,b0,b1,b2,b3,b4,b5,b6,b7,false)
return glue (s0,s1,s2,s3,s4,s5,s6,s7,c)
#8
-1
You can use zfill:
你可以使用zfill:
print str(1).zfill(2)
print str(10).zfill(2)
print str(100).zfill(2)
prints:
打印:
01
10
100
I like this solution, as it helps not only when outputting the number, but when you need to assign it to a variable... e.g. - x = str(datetime.date.today().month).zfill(2) will return x as '02' for the month of feb.
我喜欢这个解决方案,因为它不仅有助于输出数字,还有助于将其分配给变量......例如 - x = str(datetime.date.today()。month).zfill(2)将为feb月份返回x为'02'。