This question already has an answer here:
这个问题已经有了答案:
- Python int to binary? 23 answers
- Python二进制整数吗?23日答案
Is there any module or function in python I can use to convert a decimal number to its binary equivalent? I am able to convert binary to decimal using int('[binary_value]',2), so any way to do the reverse without writing the code to do it myself?
在python中是否有任何模块或函数可以用来将一个十进制数转换为它的二进制等价物?我可以使用int('[binary_value]',2)将二进制转换为十进制,因此,如果没有编写代码,我可以自己做反向操作吗?
8 个解决方案
#1
161
all numbers are stored in binary. if you want a textual representation of a given number in binary, use bin(i)
所有的数字都存储在二进制中。如果您想要二进制中给定数字的文本表示,请使用bin(i)
>>> bin(10)
'0b1010'
>>> 0b1010
10
#2
59
"{0:#b}".format(my_int)
#3
34
Without the 0b in front:
没有前面的0b:
"{0:b}".format(int)
Starting with Python 3.6 you can also use formatted string literal or f-string, --- PEP:
从Python 3.6开始,您还可以使用格式化的字符串字面量或f-string, --- PEP:
f"{int:b}"
#4
19
def dec_to_bin(x):
return int(bin(x)[2:])
It's that easy.
它是那么容易。
#5
9
You can also use a function from the numpy module
您还可以使用numpy模块中的函数。
from numpy import binary_repr
which can also handle leading zeros:
也可以处理前导零:
Definition: binary_repr(num, width=None)
Docstring:
Return the binary representation of the input number as a string.
This is equivalent to using base_repr with base 2, but about 25x
faster.
For negative numbers, if width is not given, a - sign is added to the
front. If width is given, the two's complement of the number is
returned, with respect to that width.
#6
5
I agree with @aaronasterling's answer. However, if you want a non-binary string that you can cast into an int, then you can use the canonical algorithm:
我同意@aaronasterling的回答。但是,如果您想要一个非二进制字符串,您可以将其转换为int,那么您可以使用规范算法:
def decToBin(n):
if n==0: return ''
else:
return decToBin(n/2) + str(n%2)
#7
2
n=int(input('please enter the no. in decimal format: '))
x=n
k=[]
while (n>0):
a=int(float(n%2))
k.append(a)
n=(n-a)/2
k.append(0)
string=""
for j in k[::-1]:
string=string+str(j)
print('The binary no. for %d is %s'%(x, string))
#8
1
For the sake of completion: if you want to convert fixed point representation to its binary equivalent you can perform the following operations:
为了完成:如果您想将定点表示转换为它的二进制等效项,您可以执行以下操作:
-
Get the integer and fractional part.
得到整数和小数部分。
from decimal import * a = Decimal(3.625) a_split = (int(a//1),a%1)
-
Convert the fractional part in its binary representation. To achieve this multiply successively by 2.
将分数部分转换成二进制表示形式。要连续地乘以2。
fr = a_split[1] str(int(fr*2)) + str(int(2*(fr*2)%1)) + ...
You can read the explanation here.
你可以在这里阅读解释。
#1
161
all numbers are stored in binary. if you want a textual representation of a given number in binary, use bin(i)
所有的数字都存储在二进制中。如果您想要二进制中给定数字的文本表示,请使用bin(i)
>>> bin(10)
'0b1010'
>>> 0b1010
10
#2
59
"{0:#b}".format(my_int)
#3
34
Without the 0b in front:
没有前面的0b:
"{0:b}".format(int)
Starting with Python 3.6 you can also use formatted string literal or f-string, --- PEP:
从Python 3.6开始,您还可以使用格式化的字符串字面量或f-string, --- PEP:
f"{int:b}"
#4
19
def dec_to_bin(x):
return int(bin(x)[2:])
It's that easy.
它是那么容易。
#5
9
You can also use a function from the numpy module
您还可以使用numpy模块中的函数。
from numpy import binary_repr
which can also handle leading zeros:
也可以处理前导零:
Definition: binary_repr(num, width=None)
Docstring:
Return the binary representation of the input number as a string.
This is equivalent to using base_repr with base 2, but about 25x
faster.
For negative numbers, if width is not given, a - sign is added to the
front. If width is given, the two's complement of the number is
returned, with respect to that width.
#6
5
I agree with @aaronasterling's answer. However, if you want a non-binary string that you can cast into an int, then you can use the canonical algorithm:
我同意@aaronasterling的回答。但是,如果您想要一个非二进制字符串,您可以将其转换为int,那么您可以使用规范算法:
def decToBin(n):
if n==0: return ''
else:
return decToBin(n/2) + str(n%2)
#7
2
n=int(input('please enter the no. in decimal format: '))
x=n
k=[]
while (n>0):
a=int(float(n%2))
k.append(a)
n=(n-a)/2
k.append(0)
string=""
for j in k[::-1]:
string=string+str(j)
print('The binary no. for %d is %s'%(x, string))
#8
1
For the sake of completion: if you want to convert fixed point representation to its binary equivalent you can perform the following operations:
为了完成:如果您想将定点表示转换为它的二进制等效项,您可以执行以下操作:
-
Get the integer and fractional part.
得到整数和小数部分。
from decimal import * a = Decimal(3.625) a_split = (int(a//1),a%1)
-
Convert the fractional part in its binary representation. To achieve this multiply successively by 2.
将分数部分转换成二进制表示形式。要连续地乘以2。
fr = a_split[1] str(int(fr*2)) + str(int(2*(fr*2)%1)) + ...
You can read the explanation here.
你可以在这里阅读解释。