Convert Binary to Decimal in Python


In this article, we will learn how to convert binary to decimal in Python. We will create custom functions to convert binary to decimal with different approaches.

    Table of Contents

  1. Binary to Decimal Conversion
  2. Method 1: Using int() function
  3. Method 2: Using while loop
  4. Method 3: Using for loop
  5. Method 4: Using recursion

Binary to Decimal Conversion

Binary numbers are a base-2 number system. It uses only two digits, 0 and 1. The binary number system is used in computers and other digital devices. For example, 10, 11, 101, etc.

Decimal numbers are a base-10 number system. It uses ten digits, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The decimal number system is used in everyday life. For example, 24, 45, 95, etc.

We can convert any binary number to a decimal number. To convert binary to decimal, we need to multiply each digit with its corresponding power of 2 and add the result. For example, 11002 = 1*23 + 1*22 + 0*21 + 0*20 = 8 + 4 + 0 + 0 = 1210

binary to decimal conversion
Binary to Decimal Conversion

Examples of other conversions:

(11010)2 = 1*24 + 1*23 + 0*22 + 1*21 + 0*20 = (26)10
(1001)2 = 1*23 + 0*22 + 0*21 + 1*20 = (9)10
(101)2 = 1*22 + 0*21 + 1*20 = (5)10
(111)2 = 1*22 + 1*21 + 1*20 = (7)10
(1010)2 = 1*23 + 0*22 + 1*21 + 0*20 = (10)10
(1101)2 = 1*23 + 1*22 + 0*21 + 1*20 = (13)10
(1111)2 = 1*23 + 1*22 + 1*21 + 1*20 = (15)10
(10001)2 = 1*24 + 0*23 + 0*22 + 0*21 + 1*20 = (17)10

# Method 1: Using int() Function

We can use the int() function to convert binary to decimal.

The int() function takes two arguments, the first is the number to be converted and the second is the base of the number. The default base is 10. So, we need to pass 2 as the second argument to convert binary to decimal.

Note: The number to be converted (decimal number here) should be passed as a string.

num = input("Enter a binary number: ")
num = str(num)

# convert to decimal
dec = int(num, 2)
print("The decimal value of " + str(num) + " is " + str(dec))

Output:

Enter a binary number: 110110
The decimal value of 110110 is 54

# Method 2:

In the last method, we used a built-in python function to convert binary to decimal. Now we are going to create our own program that converts a binary number to a decimal.

Algorithm

  1. Create a python function that takes a decimal number as an integer.
  2. Inside this define a variable dec and assign it 0. It stores decimal values.
  3. Define another variable named mul and assign it 1. This will be used to multiply with the current bit of binary digit.
  4. Run a loop until the binary number becomes 0. In each loop multiply the multiple with the last bit, divide the number by 10 and multiply the mul variable by 2.
  5. Finally, return the decimal value.
def bin_to_dec(n):
    dec = 0
    mul = 1
    while n:
        dec = dec + (n % 10) * mul
        n //= 10
        mul *= 2
    return dec

num = int(input("Enter a binary number: "))
print("The decimal value of " + str(num) + " is " + str(bin_to_dec(num)))

Output:

Enter a binary number: 11001
The decimal value of 11001 is 25
binary to decimal example
Binary to Decimal Example

# Method 3:

In this method, we will use enumerate() function with for loop to go through each digit of the binary number and convert it to decimal.

Algorithm

  1. Create a function. Inside convert the input number to a string first.
  2. Reverse the string using [::-1] slicing.
  3. Define a variable dec and assign it 0. It stores decimal values.
  4. Run a for loop using enumerate() function to go through each digit of the binary number.
  5. Inside the loop, convert each digit to an integer and multiply the digit with 2i where i is the index of the current digit and add it to the dec variable.
  6. Finally, return the decimal value.
def bin_to_dec(n):
    # convert the input to string
    n = str(n)
    # reverse string
    # because we will start from the right
    n = n[::-1]
    # initialize result
    dec = 0

    # loop through the string
    for i, digit in enumerate(n):
        # multiply each digit by 2^i
        dec += int(digit) * (2**i)
    return dec

num = input("Enter a binary number: ")
print(bin_to_dec(num))
binary to decimal example
Binary to Decimal Example

Output:

Enter a binary number: 10011
19

# Method 4: Using Recursion

Let's use recursion to convert binary to decimal.

For this, we need to create a base case and a recursive case. In the base case, we check if the binary number is 0. If it is, we return 0. Otherwise, we return the remainder of the binary number divided by 10 plus 2 times the function itself with the binary number divided by 10.

# binary to decimal recursive
def bin2dec(bin):
    if bin == 0:
        return 0
    else:
        return bin%10 + 2*bin2dec(bin//10)

num = int(input("Enter a binary number: "))
print("The decimal value of " + str(num) + " is " + str(bin2dec(num)))
binary to decimal example
Binary to Decimal Example

Output:

Enter a binary number: 10011
The decimal value of 10011 is 19

Stay Ahead, Learn More