在python中将十进制转换为二进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3528146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert decimal to binary in python
提问by Paul
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) 将二进制转换为十进制,那么有什么方法可以在不编写代码的情况下进行相反的操作呢?
回答by aaronasterling
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
回答by Matt Williamson
"{0:#b}".format(my_int)
回答by inspectorG4dget
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)
回答by schmidmt
def dec_to_bin(x):
return int(bin(x)[2:])
It's that easy.
就这么简单。
回答by flonk
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.
回答by harry
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))
回答by user136036
Without the 0b in front:
没有前面的 0b:
"{0:b}".format(int)
Starting with Python 3.6 you can also use formatted string literalor f-string, --- PEP:
从 Python 3.6 开始,您还可以使用格式化字符串文字或f-string--- PEP:
f"{int:b}"
回答by Kalouste
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.
fr = a_split[1] str(int(fr*2)) + str(int(2*(fr*2)%1)) + ...
获取整数和小数部分。
from decimal import * a = Decimal(3.625) a_split = (int(a//1),a%1)以二进制表示形式转换小数部分。为了实现这个乘以2。
fr = a_split[1] str(int(fr*2)) + str(int(2*(fr*2)%1)) + ...
You can read the explanation here.
您可以在此处阅读说明。

