Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript hex to decimal (BigNumber)

I need to convert large hex to dec. I already solved it using Java. Here's my solution:

String input = "253D3FB468A0E24677C28A624BE0F939";
System.out.println(new BigInteger(input, 16));

That gives me the following output: 49499458037667732112883750526794135865.

Very simple using Java's BigInteger class!!

I need the same result using Javascript! I tried:

var str = '253D3FB468A0E24677C28A624BE0F939';
console.log(parseInt(str, 16));

With output 4.949945803766773e+37.

I think JavaScript's Int isn't enough for this kind. How can i achieve the same Java's output with JavaScript? Thanks a lot!

like image 945
Sergio David Romero Avatar asked Oct 17 '25 18:10

Sergio David Romero


1 Answers

Use a Big Int library: https://github.com/peterolson/BigInteger.js

var q = new bigInt("253D3FB468A0E24677C28A624BE0F939", 16); 
console.log(q.toString());

49499458037667732112883750526794135865

like image 89
Alex K. Avatar answered Oct 20 '25 08:10

Alex K.