Python Binary To Decimal Tutorial With Examples

In this Python Binary To Decimal post we will talk about  some basics of number system and how to convert binary numbers into decimal numbers. But before doing this we will have to understand some basic concepts of binary number system. So let’s start.

Normally when we work in programming world, we work with binary system as well as with decimal system. So in the programming world we use binary system and decimal system but apart from this we have two more system which are octal and hexadecimal  system. Now when do we use all these stuffs ? In physical address 88-78-73-9E-74-38, the number is defined in hexadecimal number, same goes to IPv6 3ffe:1900:4545:3:200:f8ff:fe21:67cf. ,which is defined in hexadecimal numbers. In our computer we use binary format and in normal system we use decimal system. So let’s understand how can we convert these values system to another values system.

Python Binary To Decimal
Python Binary To Decimal

Python Binary To Decimal Tutorial – Convert Binary Numbers Into Decimal Numbers

Before we go into binary to decimal conversion process, i will  teach you some basics of Binary system.

What is Binary Number System ?

  • Binary is a number system represented by numeric values using two numbers, 0 and 1 .
  • The binary system is used internally by all the modern computers.
  • So for example – 1001 is a binary number.

Logic Behind Binary Table

Now to do the conversion process you have to understand the math behind it. If you understand the power of 2, you can do pretty much any binary conversion. So let’s understand the power of 2.

Python Binary To Decimal
Python Binary To Decimal

Memorize at least these, this will help you to do any conversion very quickly.

Binary To Decimal Conversion Process

Now we will see how to convert a binary number into decimal number. So here i am taking a binary number for example – 1011010.

Now we have to convert this number into decimal so let’s see how to do that.

Python Binary To Decimal
Python Binary To Decimal
  • We have a binary table i.e., 1, 2, 4, 8, 16, 32, 64, 128,………, if you need further then go further.
  • Now we have to write the binary number as shown in figure.
  • And then add all the numbers, this will give us a decimal number. And this way we convert binary into decimal.
  • So 90 is our result of 1011010.

Binary To Decimal Conversion Logic – What To Do

So to convert binary to decimal we have to apply the following logic.

For each digit from right to left –

  • Peel off the right digit
  • Multiply it by 2 to some power
  • Add that to the running decNum total.

So now lets to implement this logic in program.

Binary To Decimal Conversion Example

Let’s see this concept with an example. So the code for this is –

Code Explanation

  • First of all define a function that will receive  binary number.
  • Define two other variable decNum which is our decimal number equivalent and Power, both of them have to initialize with 0.
  • Then start a while loop and  repeat until binNum>0.
  • Then take modulo of given binary number with 10.
  • Multiply remaining with 2 raised to the power, it’s position from right end.
  • And then add result with previously generated result.
  • Then you have to update binary number by dividing it by 10.
  • And then increment power by 1 each time through the loop.
  • Then return the decNum.
  • And finally call the binToDec function.

Now you can see the result.

Python Binary To Decimal
Python Binary To Decimal

So, now we have converted binary numbers into decimal numbers successfully. And now we will see more methods to convert binary to decimal.

Binary To Decimal Conversion Using int() method

Now we will see how to convert binary into decimal using int() method.

  • In this example, we will ask from user to input.
  • int() method takes two argument one is  number that is to be converted and another is the base of the number to be converted, which is 2 in case of binary numbers..
  • So the code for this is following.

This is very easy and code is also a few lines, so let’s see the result.

Python Binary To Decimal
Python Binary To Decimal

Recommended Articles

So friends, this was all about Python Binary To Decimal tutorial. I hope you will liked this and if you found it helpful then please share. And if you have any query regarding this then leave your comment, i will solve your queries. And if you like tutorials of Simplified Python then tell your friends to follow this. Thanks Everyone.

Leave a Comment