Python Convert binary to decimal(without builtin function)

Python PythonBasic

In this post, we are going to learn Python convert binary to decimal without a built-in function.

1. Python Program binary to decimal


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

2. Python Program to convert binary to decomal


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

3.Python Program to convert binary to decimal


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

4. 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 calculating the decimal of binary number by multiplying num to 2 the power of number
  • The appending the result to list and calculate 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

5. 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

6. binary to decimal in python using recursion


In this example, we are using 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.