Python binaire à décimal

Exemples de code

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
7
0

N

myDecimalInteger = int("A278832", 16) #Converts to decimal from base 16
4
0

N

int(binaryString, 2)
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
0
0

N

def getBaseTen(binaryVal):
	count = 0

	#reverse the string
    binaryVal = binaryVal[::-1]

    #go through the list and get the value of all 1's
	for i in range(0, len(binaryVal)):
    	if(binaryVal[i] == "1"):
            count += 2**i
    
    return count

Dans d'autres langues

Cette page est dans d'autres langues

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