Convert binary to decimal in Python
In this post, you will learn how to convert a binary number into an equivalent decimal using the Python programming language. Here, we have mentioned three ways to convert a binary to a decimal number.
A binary number consists of two numbers 0s and 1s. It is expressed in the base-2 numeral system or binary numeral system. As the computer only understands binary language that is 0 or 1, all inputs given to a computer are decoded by it into series of 0's or 1's to process it further. Any combination of 0 and 1 is a binary number such as 1011, 100, 101011, 111011 etc.
A decimal number is a base-10 number system. It ranges from 0 to 9 i.e., 0,1,2,3,4,5,6,7,8,9. Any combination of digits is a decimal number such as 61, 22, 912, 0, 5 etc. The main advantages of the decimal number system are easy-to-read, used by humans and easy to manipulate.
Python binary to decimal using function
In the given Python program, we have defined a function to convert binary to decimal numbers. First, we have taken the modulo of the given binary number with 10 and multiplied the reminder with 2 raised to the power. Then, we added the result to the previous generated result and updated the binary number by dividing it by 10. This process will be repeated until binary > 0.
def binary_to_decimal(binary):
decimal, i = 0, 0
while(binary != 0):
x = binary % 10
decimal = decimal + x * pow(2, i)
binary = binary//10
i += 1
print(decimal)
# Driver code
if __name__ == '__main__':
binary_to_decimal(100)
binary_to_decimal(101)
binary_to_decimal(110)
binary_to_decimal(1001)
Output of the above code:
4
5
6
9
Python binary to decimal using int() function
In the given example, we have used the int() function to convert the binary to decimal. The int() function can help in converting various values to a decimal number.
def binary_to_decimal(n):
return int(n,2)
# Driver code
if __name__ == '__main__':
print(binary_to_decimal('101'))
print(binary_to_decimal('110'))
print(binary_to_decimal('1101'))
Output of the above code:
5
6
13
Python binary to decimal using for loop
In the given Python program, we have used the for loop to convert the binary to decimal.
def binary_to_decimal(binary):
decimal = 0
for digit in binary:
decimal = decimal*2 + int(digit)
print("The decimal value is:", decimal)
# Driver code
if __name__ == '__main__':
binary_to_decimal('111')
binary_to_decimal('110')
binary_to_decimal('101')
binary_to_decimal('1111')
Output of the above code:
The decimal value is: 7
The decimal value is: 6
The decimal value is: 5
The decimal value is: 15
Python binary to decimal using while loop
In the given Python program, we ask from the user to enter a number in binary number system to convert that number into decimal number system using the while loop.
print("Enter the binary number: ")
bin_num = int(input())
dec_num = 0
i = 1
while bin_num!=0:
rem = bin_num%10
dec_num = dec_num + (rem*i)
i = i*2
bin_num = int(bin_num/10)
print("\nEquivalent decimal value: ", dec_num)
Output of the above code:
Enter the binary number:
1101
Equivalent decimal value: 13
Python binary to decimal using bin()
In the given example, we have used the pre-defined method bin() to convert the binary to decimal.
# Function to convert
# Decimal number to Binary number
def decimalToBinary(n):
return bin(n).replace("0b","")
# Driver code
if __name__ == '__main__':
print(decimalToBinary(9))
print(decimalToBinary(12))
print(decimalToBinary(11))
print(decimalToBinary(22))
Output of the above code:
1001
1100
1011
10110
Python binary to decimal using Recursion
In the given Python program, we have used the recursion function to convert a binary to decimal number system.
def binToDecNumber(x):
if x == 1 or x == 0:
return x
length = len(str(x))
y = x//pow(10,length-1)
return (pow(2,length-1) * y)+ binToDecNumber(x%pow(10,length-1))
binary = int(input('Enter a Binary number: '))
decimal = binToDecNumber(binary)
print("Decimal of {p}: {q} ".format(p=binary, q=decimal))
Output of the above code:
Enter a Binary number: 11001
Decimal of 11001: 25
Related Articles
Python Numpy Array Shape
Python program to convert decimal to binary
Python convert list to numpy array
Python Numpy Array Shape
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Trigonometric functions Python Numpy
Python Pandas Dataframe to CSV
Python Pandas DataFrame
Convert list to dictionary Python
Dictionary inside list Python
Convert dictionary to dataframe Python
Python Pandas CSV to Dataframe
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module