Python Program to Convert Binary to Decimal

In this article, we've created some programs in Python, to convert binary number entered by user at run-time to its equivalent decimal value. Here are the list of programs:

Note - Before creating these programs, if you're are not aware about steps used for the conversion, refer to Binary to Decimal Conversion Steps and Formula to get every required thing about the topic.

Binary to Decimal using while Loop

To convert binary to decimal number in Python, you have to ask from user to enter a number in binary number system to convert that number into decimal number system as shown in the program given here.

The question is, write a Python program to convert binary to decimal using while loop. Here is its answer:

print("Enter the Binary Number: ")
bnum = int(input())

dnum = 0
i = 1
while bnum!=0:
    rem = bnum%10
    dnum = dnum + (rem*i)
    i = i*2
    bnum = int(bnum/10)

print("\nEquivalent Decimal Value = ", dnum)

Here is its sample run. This is the initial output:

binary to decimal python

Now supply the input say 10110 as binary number, press ENTER key to convert it into its equivalent decimal value:

convert binary to decimal python

The dry run of following block of code:

while bnum!=0:
    rem = bnum%10
    dnum = dnum + (rem*i)
    i = i*2
    bnum = int(bnum/10)

with user input, 10110 goes like:

Binary to Decimal using for Loop

This program uses for loop, instead of while. The str() method is used to convert any type of value to a string type. And the len() is used to find the length of a string. The range() returns a sequence of values from 0 to one less than the value defined as its argument. By default, each time the value gets incremented by 1.

print("Enter the Binary Number: ", end="")
bnum = int(input())

dnum = 0
m = 1
blen = len(str(bnum))

for k in range(blen):
    rem = bnum%10
    dnum = dnum + (rem * m)
    m = m * 2
    bnum = int(bnum/10)

print("\nEquivalent Decimal Value = ", dnum)

Here is its sample run with user input, 11110110:

python convert binary to decimal

Binary to Decimal using int()

This program uses int() method to convert binary to decimal. That is, we've received a value from user as input. While receiving the value using input() method, we've not specified the type.

Therefore the value entered by user, treated as a string type value. Therefore, using int(), the string type value gets converted into an integer type value of base 2, that is binary number using int(givenValue, 2) as shown in the program given below:

print("Enter the Binary Number: ", end="")
bnum = input()

dnum = int(bnum, 2)
print("\nEquivalent Decimal Value = ", dnum)

Here is its sample run with user input 110111 as binary number:

binary to decimal conversion python

Binary to Decimal using Function

This program uses a user-defined function named BinToDec() to do the same job, that is converting of an binary number to its decimal equivalent.

def BinToDec(b):
    return int(b, 2)

print("Enter the Binary Number: ", end="")
bnum = input()

dnum = BinToDec(bnum)
print("\nEquivalent Decimal Value = ", dnum)

The function, BinToDec() takes binary number as its argument, and returns its equivalent decimal value using int() with base 2

Binary to Decimal using Class

This is the last program of this article, created using a class named CodesCracker, an object-oriented feature of Python.

class CodesCracker:
    def BinToDec(self, b):
        return int(b, 2)

print("Enter the Binary Number: ", end="")
bnum = input()

ob = CodesCracker()
dnum = ob.BinToDec(bnum)
print("\nEquivalent Decimal Value = ", dnum)

An object, ob is created of class CodesCracker to access its member function named BinToDec() using dot (.) operator. Rest of the things works like a normal function.

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »


Liked this post? Share it!