In this post, we are going to learn how to convert binary to decimal in Python with examples by using built-in functions and without built-in functions.
1. How to convert binary to decimal in python inbuilt function
In this example we are using Python built-in int() function returns an integer from a string or number. If no argument is passed then it returns 0 bypasses the base 2 to find decimal of binary number.
Python Program to convert binary to decimal
bin_num = input('Enter a Binary number: ')
decimal_num = int(bin_num,2)
print("binary to Decimal val is : ",decimal_num)
Output
Enter a Binary number: 1111
binary to Decimal val is : 15
2. binary to decimal in python without inbuilt function
In this example, we have defined a function BinaryToDecimal() that takes a binary number as an argument, and for loop to iterate over each digit of the binary number to calculate decimal multiply each digit to 2 by adding the number.
bin_to_dec*2 + int(num)
1*2+1 =1
1*2+1 = 3
2*3+1 = 7
2*7+1 =15
Python Program Example
binary = '1111'
def BinaryToDecimal(binary):
bin_to_dec = 0
for num in binary:
bin_to_dec = bin_to_dec*2 + int(num)
print("binary to decimal value is :", bin_to_dec)
BinaryToDecimal(binary)
Output
binary to decimal value is : 15
3. Binary to decimal in Python using function
In this example, we have defined a function BinaryToDecimal() and iterated over the binary digit in reversed order, and calculated decimal number by using the formula decimal_val += 2**count * int(num).
binary = '1111'
def BinaryToDecimal(binary):
decimal_val = 0
count = 0
for num in reversed(binary):
decimal_val += 2**count * int(num)
count+=1
print ('binary to decimal value is: ', decimal_val)
BinaryToDecimal(binary)
Output
binary to decimal value is : 15
4. Program to convert binary to decimal in Python
In this Python program, we are using the range loop to iterate over the binary number length
- We have defined a function BinaryToDec().
- We are using this formula to and calculate the decimal dec_val += int(binary[num]) * 2**abs((num – (len(binary) – 1)))
Python Program Example
binary = '1111'
def BinaryToDec(binary):
dec_val = 0
for num in range(len(binary)):
dec_val += int(binary[num]) * 2**abs((num - (len(binary) - 1)))
print('binary to decimal:',dec_val)
Output
binary to decimal: 15
5. binary string to decimal in python
In this example, we have a binary number as a string and we have defined a function binaryToDec().
- First, we have converted the binary number to list
- Using for range loop to iterate over the list and claculating the decimal of binary number by mutiplying num to 2 power of number
- The appending the result to result list and calulate the sum to get the decimal number.
Python Program Example
binary = '1111'
def binaryToDec(bin_num):
array = [int(num) for num in bin_num]
array = array[::-1]
reslst = []
for num in range(len(array)):
reslst.append(array[num]*(2**num))
Dec_num = sum(reslst)
print('binary to Decimal value is : ',Dec_num)
binaryToDec(binary)
Output
binary to Decimal value is : 15
6. While loop to convert binary to decimal
In this python program Example, we are using the while loop to convert binary numbers to decimals.
Python Program Example
binary = int(input('Please enter binary number:'))
def BinaryDecimal(bin_num):
num,dec_num, base = bin_num, 0, 1
t_num = num
while(t_num):
digit = t_num % 10
t_num = int(t_num / 10)
dec_num += digit * base
base = base * 2
print('The binary to decimal value is =', dec_num)
BinaryDecimal(binary)
Output
The binary to decimal value is = 15
7. Binary to decimal in Python using recursion
In this example, we used recursion to define a function and call this function inside the same function to find the decimal of binary number.
Python Program Example
def binaryToDec(bin_num):
if bin_num == 1 or bin_num == 0:
return bin_num
Strlen = len(str(bin_num))
digit = bin_num//pow(10,Strlen-1)
return (pow(2,Strlen-1) * digit)+ binaryToDec(bin_num%pow(10,Strlen-1))
bin_num = int(input('Enter a Binary number: '))
decimal = binaryToDec(bin_num)
print("binary to Decimal val is : ",decimal)
Output
Enter a Binary number: 1111
binary to Decimal val is : 15
Summary
In this post, we have learned How to convert binary to decimal in Python using built-in function and without built-in function