DEV Community

Mallaya
Mallaya

Posted on • Updated on

Convert binary to decimal

I have just worked on a project where I had to create a binary to decimal converter. For the same project, I developed other binary, hex, and octal conversion tools. For my machine learning project, I developed a binary to decimal converter. My client insisted to develop such to enhance security for the project.

Honestly, considering recent security threats, it is really important to enhance security for an app.

When I came to know about developing this binary to decimal converter, I had no idea who I would do that because I had very little knowledge about this number conversion system. I spent one full day learning about it. For that reason, I thought, sharing code for such conversion can be beneficial for the developer who may work on such a project in the future.

About binary and decimal numbers:

Binary numbers are written in just zeros and ones. They are the 2-bit numbers and all the electronic devices and computer processors can understand binary numbers only. For the last several years, binary numbers have been used by almost all devices.

Decimal numbers are the base-10 numbers. It contains 0 to 9 numbers. After binary numbers, decimals are the most used numbering system right now. Most machine learning, artificial intelligence products, and IoT products use this numbering system.

Why did I need to develop a binary to the decimal converter?

As decimal numbers are represented in 0 to 9 and binary contain zeros and ones. Decimal numbers can easily be read and understood by humans while it is pretty difficult to read the convert binary numbers. For that reason, my client wanted me to develop such a tool.

Javascript code to convert binary to decimal:

function bintodec() {
    var input = document.getElementById("text_value").value;
        var cc = new BigNumber(input, 2);

document.getElementById("ans").innerHTML = cc;
       }
Enter fullscreen mode Exit fullscreen mode

And done! Copy and paste the above code to convert binary to decimal. In this code, I used the Bigumber.js javascript library. If you are dealing with such big numbers, then you need to use Bignumber.js otherwise you might get an error while getting the answer.
So, this is the technique I used to convert binary to decimal. If you have used any other technique for such conversion, then do let me know by dropping a comment. I would love to hear from you.

Top comments (0)