Konwersja liczby całkowitej do binarnej w Pythonie

Aby przekonwertować liczbę całkowitą na binarną, użyłem tego kodu:

>>> bin(6)  
'0b110'

A kiedy wymazać '0b', używam tego:

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

Co mogę zrobić, jeśli chcę pokazać 6 jako 00000110 zamiast 110?

Author: jamylak, 2012-05-02

8 answers

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

Aby wyjaśnić Części ciągu formatowania:

  • {} umieszcza zmienną w łańcuchu
  • 0 przyjmuje zmienną na pozycji argumentu 0
  • : dodaje opcje formatowania dla tej zmiennej (w przeciwnym razie reprezentowałaby dziesiętne 6)
  • 08 Formatuje liczbę na osiem cyfr zero-wyściełane po lewej
  • b zamienia liczbę na jej binarną reprezentację
 256
Author: eumiro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-13 07:21:47

Just another idea:

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

Shorter way via string interpolation (Python 3.6+):

>>> f'{6:08b}'
'00000110'
 80
Author: mshsayem,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-10 06:33:29

Metoda nieco przekręcająca...

>>> bin8 = lambda x : ''.join(reversed( [str((x >> i) & 1) for i in range(8)] ) )
>>> bin8(6)
'00000110'
>>> bin8(-3)
'11111101'
 15
Author: sobel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-18 11:41:29

Wystarczy użyć funkcji format

format(6, "08b")

Ogólna forma to

format(<the_integer>, "<0><width_of_string><format_specifier>")
 10
Author: theOne,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-07 20:16:08

ODPOWIEDŹ Eumiro jest lepsza, jednak zamieszczam to dla odmiany:

>>> "%08d" % int(bin(6)[2:])
00000110
 5
Author: jedwards,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-02 09:35:17

.. lub jeśli nie jesteś pewien, czy zawsze powinno być 8 cyfr, możesz podać go jako parametr:

>>> '%0*d' % (8, int(bin(6)[2:]))
'00000110'
 5
Author: thebjorn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-02 09:47:30

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))
 0
Author: Shadychiri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-08 16:40:11

('0' * 7 + bin(6)[2:])[-8:]

Lub

right_side = bin(6)[2:] '0' * ( 8 - len( right_side )) + right_side

 0
Author: zerg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-05 00:07:31