Reputation: 2699
This question is more complicated than it first looks - although still conceptually fairly simple.
I am representing big numbers, which are outside of javascript's max range, as strings of hexadecimal digits (this is in accordance with the approach of node-mysql's bigNumberStrings
option).
I simply want to know how can I convert a string of hexadecimal digits into a string of decimal digits of the same numerical value? Obviously, there is a complexity in doing this arising from the fact that you cannot do maths with javascript with large numbers.
I have done a significant amount of searching for a standard solution although I haven't found one yet.
Upvotes: 9
Views: 11943
Reputation: 539
The other answers in this thread didn't work for me, but this one did:
function h2d(s) {
function add(x, y) {
var c = 0, r = [];
var x = x.split('').map(Number);
var y = y.split('').map(Number);
while(x.length || y.length) {
var s = (x.pop() || 0) + (y.pop() || 0) + c;
r.unshift(s < 10 ? s : s - 10);
c = s < 10 ? 0 : 1;
}
if(c) r.unshift(c);
return r.join('');
}
var dec = '0';
s.split('').forEach(function(chr) {
var n = parseInt(chr, 16);
for(var t = 8; t; t >>= 1) {
dec = add(dec, dec);
if(n & t) dec = add(dec, '1');
}
});
return dec;
}
Upvotes: 0
Reputation: 3078
The algorithm itself is quite simple, so no need for a special library, it actually does what you would do on paper, so it is not too inefficient.
function hexToDec(s) {
var i, j, digits = [0], carry;
for (i = 0; i < s.length; i += 1) {
carry = parseInt(s.charAt(i), 16);
for (j = 0; j < digits.length; j += 1) {
digits[j] = digits[j] * 16 + carry;
carry = digits[j] / 10 | 0;
digits[j] %= 10;
}
while (carry > 0) {
digits.push(carry % 10);
carry = carry / 10 | 0;
}
}
return digits.reverse().join('');
}
How it works: basically reads hex digits and adds them to the intermediate array of dec value computed so far. Every new hex digit means that the array of dec digits is multiplied by 16 and all carry-overs are distributed to higher order digits. The while loop it to add any additional carry-over as new digits in the array. The last line is just converting the array of dec digits to a string.
Update: Of course, you can make the algorithm more efficient by replacing number 10 everywhere by any other power of 10 (100, 1000, 10000, 100000, ...) and it will work the same. The only requirement that the intermediate values do not exceed mantissa precision (52 bits).
Upvotes: 4
Reputation: 23472
You could use a standard library like bignumber.js
Javascript
var hex = 'ABCDEF1234567890',
bignumber = new BigNumber(hex, 16);
console.log(bignumber.toString(10));
Output
12379813812177893520
On jsFiddle
Upvotes: 5