DEV Community

Mallaya
Mallaya

Posted on • Updated on

Develop a full Binary Converter With Javascript

It has been over six months since I'm working on the IoT project where firstly, I created a binary to the decimal converter and now a binary converter. When I started working on the project I had no idea that I had to create a binary converter that converts binary to hexadecimal, decimal, and octal.

Honestly, I used to find binary translation boring when I was at school. And when I had to develop a binary converter, I found it difficult. So, I decided to share the code of a binary converter here, so that anyone with the need can use it to develop a tool easily.

Yes, you may find the code online especially on StackOverflow. But the code there does not return the correct answer for big numbers. I did not know about this issue with the code until my client drew my attention. So, I have decided to write a code that returns a correct answer for large numbers as well.

A binary converter can be useful to enhance the security of a website or a file. If you prefer to save the passwords in the excel sheet then anyone having access to that file can read and memorize the password. The best solution to hide sensitive data is to use a binary converter. With the help of a binary converter, you can convert the private data into binary.

It is really easy for a machine to read the binary data but it is very difficult for a human to read and memorize the binary. Since it contains just zeros and ones, even a word represented in binary becomes very long and unreadable.

Binary converter:

HTML:

<input type="text" id="text_value1" placeholder="Enter a number or name that you want to convert"/><br><br>
              <button onclick="binarytotextconversion(); bintodecconversion(); bintooctconversion(); bintohexconversion();">Convert</button>
<div id="ans2"></div>
<div id="ans3"></div>
<div id="ans4"></div>
Enter fullscreen mode Exit fullscreen mode

Javascript Function:

  function bintodecconversion() {
    var getinput = document.getElementById("text_value1").value;
      var storeinput = parseInt(getinput)
      var finalans = new BigNumber(parseInt(getinput, 2))
      if (isNaN(finalans)) {
        document.getElementById("ans2").innerHTML = "This number cannot be converted into Decimal";
      } else {
        document.getElementById("ans2").innerHTML = finalans;
      }
    }
    function bintooctconversion() {
    var xxx = document.getElementById("text_value1").value;
    var bintooct = parseInt(xxx, 2).toString(8)
    if (isNaN(bintooct)) {
      document.getElementById("ans3").innerHTML = "This number cannot be converted into Octal";
    } else {
      document.getElementById("ans3").innerHTML = bintooct;
    }
  }
  function bintohexconversion() {
    var getvalue= document.getElementById("text_value1").value;
var processvalue = new BigNumber(conversion, 2)
var hexanswer = processvalue.toString(16)
if (isNaN(hexanswer)) {
document.getElementById("ans4").innerHTML = "This number cannot be converted into Hex";
} else {
document.getElementById("ans4").innerHTML = hexanswer;
}
    }

Enter fullscreen mode Exit fullscreen mode

In the code, I have used the big number javascript library to convert big numbers into decimal, hexadecimal, and octal. If we do not use this library then it becomes difficult for Javascript to perform such conversion. After reading this article, feel free to share anything regarding binary converter with me.

Github: https://github.com/aaryatables/binarytranslator

Top comments (1)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

A binary translator can be useful to enhance the security of a website or a file. If you prefer to save the passwords in the excel sheet then anyone having access to that file can read and memorize the password. The best solution to hide sensitive data is to use a binary translator. With the help of a binary translator, you can convert the private data into binary.

This is the worst advise on password security I have ever read. Please don't ever do this.