Convert Decimal to Binary using Python

July 12, 2022 2022-12-19 19:26Convert Decimal to Binary using Python

**Introduction of ****Convert ****Decimal To Binary **

One of the most essential components of the architecture of computers and other digital systems is the binary number system. Binary is the language used by computers and other digital systems to understand and communicate with one another, much like humans use languages to do so. It is a number system with a base of 2, with only two values (0 and 1) and ON and OFF states corresponding to those numbers. Your computer can grasp this system.

In the same way that an average person has ten fingers to symbolize a straightforward numerical system known as decimal, computers have these ON and OFF states to stand in for binary. Therefore, in order to comprehend and make sense of binary, we require a method that can transform binary code into decimal code (code that is legible by humans) and vice versa. As a result, the process of converting from decimal to binary and vice versa will be covered in this article using Python, which is one of the leading computer programming languages.

**Understanding Decimal and Binary Numbers**

The Decimal System (Base-10) contains ten numbers, which is from 0 to 9, and then utilizes their pairings to build digits, with each digit being valued ten times greater than the last digit proceeding from left to right (1, 10, 100, and so on).

**Assume a number 578 :**

In this case, the number 578 is formed by combining digits spanning from 0 to 9 to create each individual digit.

Moving from left to right, each successive digit is ten times more than the one before it.

The Binary System (Base-2) is comparable as well. It is a combination of the digits 0 and 1, with each subsequent digit having a value that is two times greater than the value of the digit that came before it (1, 2, 4, and so on).

Digits | ‘N’th Digit | 5th Digit | 4th Digit | 3rd DIGIT | 2nd Digit | 1st Digit |

Number range | 0 to 9 | 0 to 9 | 0 to 9 | 0 to 9 | 0 to 9 | 0 to 9 |

Base 10 | 10^n | 10^4 | 10^3 | 10^2 | 10^1 | 10^0 |

Digits | ‘N’th Digit | 5th Digit | 4th Digit | 3rd DIGIT | 2nd Digit | 1st Digit |

Number range | 0 or 1 | 0 or 1 | 0 or 1 | 0 or 1 | 0 or 1 | 0 or 1 |

Base 10 | 2^n | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |

**Converting from Binary to Decimal**

Using Python Since we are already familiar with the fact that the Binary System is a combination of [0 or 1] and that each digit has a value that is two times greater than the value of the previous digit, let’s examine how this knowledge can assist us in converting from binary to its decimal equivalent.

Number | 0 | 1 | 1 | 1 | 1 |

Value | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |

Therefore , (01111)^2 = (0×2^4) + (1×2^3) + (1×2^2) + (1×2^1) + (1×2^0) = (0)+(8)+(4)+(2)+(1) =(15)to the base 10

**Python Code to Convert Binary to Decimal Values**

We will examine how to use a built-in function in Python to perform the conversion from binary to decimal.

Converting Binary Values to Decimal in Python Using the Built-in Function:

We can convert a binary number to its corresponding decimal value by using the int() method in Python. The int() function requires two arguments: a value and the base of the number that is to be transformed. The base of a binary number is 2, thus the int() function needs to know that.

**Syntax: int( <value>, <base> )**

**Code:**

*# Function Binary to Decimal number *

def binaryToDecimal(val):

return int(val, 2)

*# Driver code *

if __name__ == ‘__main__’:

print(binaryToDecimal(‘101’))

print(binaryToDecimal(‘111’))

print(binaryToDecimal(‘1000’))

**Output:**

5

7

8

**Decimal to Binary Conversion in Python**

Let’s try to comprehend the Decimal to binary conversion. The simplest option to convert the decimal integers to their binary counterpart is the Division by 2.

In the approach known as “Division by 2,” we keep dividing a decimal number by 2 and writing down the remainder until we reach the value 1 as our input data. After then, we read the reminders that were written down backward to obtain the final binary value.

Let’s dissect the earlier remarks to acquire additional clarity. Imagine for a moment that we had a unique function that, given a number as input, divides that number by 2 and returns the remainder as the result. In order to convert decimal to binary, we first need to run this particular method several times before we can obtain the value 1 as the input. The final step is to print out all of the previously saved reminders in order to obtain the binary (base-2) value.

**Python Functions for Converting Decimal Numbers to Binary**

We will now examine how to program decimal to binary conversion in Python. First, we’ll attempt to implement the method we learned utilizing a unique Python recursive function call.

**To convert Decimal to Binary by Custom Recursive Function in Python**

In this demonstration, we will first create the specialized function (DecimalToBinary) that will be used to retrieve the quotients (which will be the input for the subsequent function call) and the remainder (which will be the output value). After that, we will keep calling it until the input value is higher than or equal to 1.

**Code:**

#Recursive Function to convert Decimal to Binary

def decimalToBinary(ip_val):

if ip_val >= 1:

# recursive function call

decimalToBinary(ip_val // 2)

# printing remainder from each function call

print(ip_val % 2, end = ”)

# Driver Code

if __name__ == ‘__main__’:

# decimal value

ip_val = 15

# Calling special function

decimalToBinary(ip_val)

**Output**

01111

The built-in function for converting decimal to binary is also provided by Python.

**To convert Binary to Decimal with Built-in Function in Python**

Python has a built-in method called bin() that can be used for converting a decimal value to its matching binary value. The bin() accepts a value as its parameter and delivers a binary equivalent.

Note: bin() returns a binary value with the prefix 0b, so based on the use-case, formatting must be done by removing 0b.

**Code:**

*# Function to convert decimal to binary*

*# using built-in python function*

def decimalToBinary(n):

*# converting decimal to binary*

*# and removing the prefix(0b)*

return bin(n).replace(“0b”, “”)

*# Driver code*

if __name__ == ‘__main__’:

*# calling function*

*# with decimal argument*

print(decimalToBinary(81))

**Output:**

1010001

As an alternative to utilizing the Python built-in method, we can also convert Decimal to Binary in some other way.

**To Convert Decimal to Binary in Python Without using Built-in Function**

**Code:**

*# Function to convert Decimal to Binary*

def decimalToBinary(n):

return “{0:b}”.format(int(n))

*# Driver code*

if __name__ == ‘__main__’:

print(decimalToBinary(81))

**Output:**

1010001

**Conclusion**

In this chapter, we have gone over the process of converting decimal numbers to binary in Python, both with and without the use of built-in python methods.