Číslo na binární python

Příklady kódu

9
0

N

a = 5
#this prints the value of "a" in binary
print(bin(a))
8
0

N

# Python program to convert decimal to binary 
    
# Function to convert Decimal number  
# to Binary number  
def decimalToBinary(n):  
    return bin(n).replace("0b", "")  
    
# Driver code  
if __name__ == '__main__':  
    print(decimalToBinary(8))  
    print(decimalToBinary(18))  
    print(decimalToBinary(7))  
    
Output:
1000
1001
4
0

N

print('{0:b}'.format(3))        # '11'
print('{0:8b}'.format(3))       # '      11'
print('{0:08b}'.format(3))      # '00000011'

def int2bin(integer, digits):
    if integer >= 0:
        return bin(integer)[2:].zfill(digits)
    else:
        return bin(2**digits + integer)[2:]
print(int2bin(3, 6))            # '000011'
3
0

N

a = 10
#this will print a in binary
bnr = bin(a).replace('0b','')
x = bnr[::-1] #this reverses an array
while len(x) < 8:
    x += '0'
bnr = x[::-1]
print(bnr)
1
0

N

bin(19) # will return '0b10011'

#or you can do

def binary(num):
    array = []

    #makes the binary value
    while(num > 0):
        sol1 = num / 2

        check = sol1 - int(sol1)

        if(check > 0):
            array.append("1")
            num = sol1 - 0.5
        else:
            array.append("0")
            num = sol1
            
    #makes sure the binary value is a minimum of 4 bits long
    while(len(array) < 4):
        array.append("0")

    #reverses the array
    array = array[::-1]

    #joins the array into a string, return the string
    string = ''.join(array)

    return string
  
#binary(19) will return 10011

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................