javascripthex

How to convert a very large hex number to decimal in javascript


I am trying without much success to convert a very large hex number to decimal. My problem is that using decimal = parseInt(hex, 16) gives me errors in the number when I try to convert a hex number above 14 digits.

I have no problem with this in Java, but Javascript does not seem to be accurate above 14 digits of hex.

I have tried "BigNumber" but this gives me the same erroneous result.

I have trawled the web to the best of my ability and found web sites that will do the conversion but cannot figure out how to do the conversion longhand.

I have tried getting each character in turn and multiplying it by its factor i.e. 123456789abcdef 15 * Math.pow(16, 0) + 14 * Math.pow(16, 1).... etc but I think (being a noob) that my subroutines may not have been all they should be because I got a completely (and I mean really different!) answer.

If it helps you guys I can post what I have written so far for you to look at but I am hoping someone has simple answer for me.

   <script>
   function Hex2decimal(hex){

   var stringLength = hex.length;
   var characterPosition = stringLength;
   var character;

   var hexChars = new Array();
   hexChars[0] = "0";
   hexChars[1] = "1";
   hexChars[2] = "2";
   hexChars[3] = "3";
   hexChars[4] = "4";
   hexChars[5] = "5";
   hexChars[6] = "6";
   hexChars[7] = "7";
   hexChars[8] = "8";
   hexChars[9] = "9";
   hexChars[10] = "a";
   hexChars[11] = "b";
   hexChars[12] = "c";
   hexChars[13] = "d";
   hexChars[14] = "e";
   hexChars[15] = "f";

   var index = 0;
   var hexChar;
   var result;

   //   document.writeln(hex);

while (characterPosition >= 0)
{
   //   document.writeln(characterPosition);
character = hex.charAt(characterPosition);

    while (index < hexChars.length)
    {
   //       document.writeln(index);
    document.writeln("String Character = " + character);
    hexChar = hexChars[index];
    document.writeln("Hex Character = " + hexChar);
    
        if (hexChar == character)
        {
        result = hexChar;
        document.writeln(result);
        }
    
    index++
    }

   //   document.write(character);
characterPosition--;
}

return result;
   }
   </script>

Thank you.

Paul


Solution

  • Ok, let's try this:

    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;
    }
    

    Test:

    t = 'dfae267ab6e87c62b10b476e0d70b06f8378802d21f34e7'
    console.log(h2d(t)) 
    

    prints

    342789023478234789127089427304981273408912349586345899239
    

    which is correct (feel free to verify).