如何在javascript中将非常大的十六进制数转换为十进制数

PAU*_*ITE 8 javascript hex

我正在努力将非常大的十六进制数转换为十进制数.我的问题是,当我尝试转换14位以上的十六进制数时,使用deciaml = parseInt(十六进制,16)会给出数字错误.

我在Java中没有这个问题,但Javascript在十六进制的14位数之上似乎不准确.

我试过"BigNumber",但是这给了我同样的错误结果.

我已尽最大努力浏览网页,并找到了可以进行转换的网站,但无法弄清楚如何进行转换.

我已经尝试依次获取每个角色并将其乘以其因子,即123456789abcdef 15*Math.pow(16,0)+ 14*Math.pow(16,1)....等我认为(作为菜鸟)我的子程序可能不是他们应该的全部,因为我得到了一个完全(我的意思是真的不同!)的答案.

如果它可以帮助你们,我可以发布我到目前为止所写的内容供你查看,但我希望有人对我有简单的答案.

   <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>
Run Code Online (Sandbox Code Playgroud)

谢谢.

保罗

geo*_*org 15

好的,让我们试试这个:

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;
}
Run Code Online (Sandbox Code Playgroud)

测试:

t = 'dfae267ab6e87c62b10b476e0d70b06f8378802d21f34e7'
console.log(h2d(t)) 
Run Code Online (Sandbox Code Playgroud)

版画

342789023478234789127089427304981273408912349586345899239
Run Code Online (Sandbox Code Playgroud)

这是正确的(随意验证).


Coo*_*J86 14

新的“n”简单方法

var hex = "7FDDDDDDDDDDDDDDDDDDDDDD";
if (hex.length % 2) { hex = '0' + hex; }

var bn = BigInt('0x' + hex);

var d = bn.toString(10);
Run Code Online (Sandbox Code Playgroud)

BigInts 现在在 node.js 和 Chrome 中都可用。Firefox 应该不会落后太多。

如果您需要处理负数,则需要做一些工作:

本质上:

function hexToBn(hex) {
  if (hex.length % 2) {
    hex = '0' + hex;
  }

  var highbyte = parseInt(hex.slice(0, 2), 16)
  var bn = BigInt('0x' + hex);

  if (0x80 & highbyte) {
    // You'd think `bn = ~bn;` would work... but it doesn't

    // manually perform two's compliment (flip bits, add one)
    // (because JS binary operators are incorrect for negatives)
    bn = BigInt('0b' + bn.toString(2).split('').map(function (i) {
      return '0' === i ? 1 : 0
    }).join('')) + BigInt(1);
    bn = -bn;
  }

  return bn;
}
Run Code Online (Sandbox Code Playgroud)