Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - hexadecimal string to decimal string

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.

like image 968
Joshua Bambrick Avatar asked Feb 10 '14 00:02

Joshua Bambrick


People also ask

How do you convert hexadecimal to decimal?

Given hexadecimal number is 7CF. To convert this into a decimal number system, multiply each digit with the powers of 16 starting from units place of the number. From this, the rule can be defined for the conversion from hex numbers to decimal numbers.

How do you use hexadecimal in JavaScript?

Uses of Hexadecimal Numbers in JavaScriptJavaScript supports the use of hexadecimal notation in place of any integer, but not decimals. As an example, the number 2514 in hex is 0x9D2, but there is no language-supported way of representing 25.14 as a hex number.

What is hexadecimal code?

Hexadecimal is a numbering system with base 16. It can be used to represent large numbers with fewer digits. In this system there are 16 symbols or possible digit values from 0 to 9, followed by six alphabetic characters -- A, B, C, D, E and F.


2 Answers

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

like image 199
Xotic750 Avatar answered Sep 21 '22 21:09

Xotic750


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).

like image 23
jJ' Avatar answered Sep 24 '22 21:09

jJ'