It's been one year since I am working on an IOT app and finally the project is going to finish. But still, this project is giving me headaches in different ways.
Just recently, my client wanted me to develop a hexadecimal to decimal converter. I was really bad at these conversions during my school. I used to use the online tools for such conversions and now I had to develop such hexadecimal to decimal tool. So, I did research and study for a day and then started developing it.
I thought it would be great if I share the Javascript code here to develop a hex to the decimal tool then it would be beneficial for those working on such converters.
function hexadecimaltodecimal() {
var input_number = document.getElementById("input_value").value;
var initialconversion = new BigNumber(input_number , 16);
var finalanswer = initialconversion.toString(10);
document.getElementById("ans").innerHTML = finalanswer;
}
In this article, we have used the bignumber.js javascript library that deals with big numbers. If you don't use the library, you might not get the correct answer for large numbers. Copy the following code and paste it into the head tag for bignumber.js.
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/8.0.2/bignumber.min.js" integrity="sha512-7UzDjRNKHpQnkh1Wf1l6i/OPINS9P2DDzTwQNX79JxfbInCXGpgI1RPb3ZD+uTP3O5X7Ke4e0+cxt2TxV7n0qQ==" crossorigin="anonymous"></script>
Top comments (2)
This code does not return correct answer for larger numbers. That's why we use bignumber
I think it is because they use it also for large numbers (not only for conversion).