js 转换数字为decmail,如何在javascript中将十六进制数转换为十进制数

博客作者遇到了在JavaScript中将超过14位的十六进制数字转换为十进制时出现错误的问题。尝试使用`parseInt()`和`BigNumber`库都未能得到准确结果。作者分享了自己的尝试,包括逐字符处理并乘以对应权重,但得到的答案不正确。最终,提供了一个名为`h2d`的函数,通过位运算实现了正确的转换,并用一个测试用例验证了该函数的准确性。
摘要由CSDN通过智能技术生成

I am trying without much success to convert a very large hex number to decimal.

My problem is that using deciaml = 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 tis 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 hev 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.

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;

}

Thank you.

Paul

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值