在python中将整数转换为二进制

时间:2022-12-02 20:56:16

In order to convert an integer to a binary, i have used this code :

为了将整数转换为二进制,我使用了以下代码:

>>> bin(6)  
'0b110'

and when to erase the '0b', i use this :

什么时候擦掉0b,我用这个:

>>> bin(6)[2:]  
'110'

What can i do if i want to show 6 as 00000110 instead of 110?

如果我想显示6是00000110而不是110,我该怎么办?

7 个解决方案

#1


239  

>>> '{0:08b}'.format(6)
'00000110'

Just to explain the parts of the formatting string:

只是为了解释格式化字符串的部分:

  • {} places a variable into a string
  • {}将变量放置到字符串中
  • 0 takes the variable at argument position 0
  • 0在参数位置0取变量。
  • : adds formatting options for this variable (otherwise it would represent decimal 6)
  • :为该变量添加格式选项(否则将表示decimal 6)
  • 08 formats the number to eight digits zero-padded on the left
  • 08格式的数字为8位零填充在左边
  • b converts the number to its binary representation
  • b将数字转换为它的二进制表示形式。

#2


78  

Just another idea:

另一个想法:

>>> bin(6)[2:].zfill(8)
'00000110'

Shorter way via string interpolation (Python 3.6+):

通过字符串插值(Python 3.6+)实现更短的方式:

>>> f'{6:08b}'
'00000110'

#3


14  

A bit twiddling method...

有点玩弄方法……

>>> bin8 = lambda x : ''.join(reversed( [str((x >> i) & 1) for i in range(8)] ) )
>>> bin8(6)
'00000110'
>>> bin8(-3)
'11111101'

#4


9  

Just use the format function

只需使用format函数

format(6, "08b")

The general form is

一般的形式是

format(<the_integer>, "<0><width_of_string><format_specifier>")

#5


5  

.. or if you're not sure it should always be 8 digits, you can pass it as a parameter:

. .或者如果你不确定它应该是8位数字,你可以把它作为参数传递:

>>> '%0*d' % (8, int(bin(6)[2:]))
'00000110'

#6


4  

eumiro's answer is better, however I'm just posting this for variety:

eumiro的回答更好,但我只是把它贴在《variety》杂志上:

>>> "%08d" % int(bin(6)[2:])
00000110

#7


0  

Going Old School always works

上老学校总是有用的

def intoBinary(number):
binarynumber=""
if (number!=0):
    while (number>=1):
        if (number %2==0):
            binarynumber=binarynumber+"0"
            number=number/2
        else:
            binarynumber=binarynumber+"1"
            number=(number-1)/2

else:
    binarynumber="0"

return "".join(reversed(binarynumber))

#1


239  

>>> '{0:08b}'.format(6)
'00000110'

Just to explain the parts of the formatting string:

只是为了解释格式化字符串的部分:

  • {} places a variable into a string
  • {}将变量放置到字符串中
  • 0 takes the variable at argument position 0
  • 0在参数位置0取变量。
  • : adds formatting options for this variable (otherwise it would represent decimal 6)
  • :为该变量添加格式选项(否则将表示decimal 6)
  • 08 formats the number to eight digits zero-padded on the left
  • 08格式的数字为8位零填充在左边
  • b converts the number to its binary representation
  • b将数字转换为它的二进制表示形式。

#2


78  

Just another idea:

另一个想法:

>>> bin(6)[2:].zfill(8)
'00000110'

Shorter way via string interpolation (Python 3.6+):

通过字符串插值(Python 3.6+)实现更短的方式:

>>> f'{6:08b}'
'00000110'

#3


14  

A bit twiddling method...

有点玩弄方法……

>>> bin8 = lambda x : ''.join(reversed( [str((x >> i) & 1) for i in range(8)] ) )
>>> bin8(6)
'00000110'
>>> bin8(-3)
'11111101'

#4


9  

Just use the format function

只需使用format函数

format(6, "08b")

The general form is

一般的形式是

format(<the_integer>, "<0><width_of_string><format_specifier>")

#5


5  

.. or if you're not sure it should always be 8 digits, you can pass it as a parameter:

. .或者如果你不确定它应该是8位数字,你可以把它作为参数传递:

>>> '%0*d' % (8, int(bin(6)[2:]))
'00000110'

#6


4  

eumiro's answer is better, however I'm just posting this for variety:

eumiro的回答更好,但我只是把它贴在《variety》杂志上:

>>> "%08d" % int(bin(6)[2:])
00000110

#7


0  

Going Old School always works

上老学校总是有用的

def intoBinary(number):
binarynumber=""
if (number!=0):
    while (number>=1):
        if (number %2==0):
            binarynumber=binarynumber+"0"
            number=number/2
        else:
            binarynumber=binarynumber+"1"
            number=(number-1)/2

else:
    binarynumber="0"

return "".join(reversed(binarynumber))