【问题标题】:How to get a BigInteger from an array of bytes in Javascript如何从 Javascript 中的字节数组中获取 BigInteger
【发布时间】:2013-06-18 13:35:20
【问题描述】:

这是我的问题:

我有一个 base64 编码的字符串,一个很长的字符串(344 个字符)。
当我解码这个字符串时,我可以获得一个字节数组。

假设我的 base64 编码字符串是ALVuSLbT
解码后得到以下数组:[0, 181, 110, 72, 182, 211]

我需要得到这个数字:779239339731(原始编码值)。
我知道如何手动执行此操作。
000000000000000000000000000000000000000000000000 000000001011010100000000000000000000000000000000 000000000000000001101110000000000000000000000000 000000000000000000000000010010000000000000000000 000000000000000000000000000000001011011000000000 000000000000000000000000000000000000000011010011

通过这个简单的示例,我可以轻松地使用 parseInt(binaryString, 2) 并将数字相加。但是我的原始字符串太大了。

我想得到一个可以将这个数字转换为字符串的大数字('779239339731')。
我没有找到一种方法来执行此操作,也没有找到允许我传递字节数组来创建 BigInteger 对象的 BigInteger/BigNumber javascript 库。

您可以查看此jsfiddle

有人可以帮我解决这个问题吗?有没有办法在 Javascript 中处理这样的事情?

【问题讨论】:

    标签: javascript biginteger


    【解决方案1】:

    我不明白为什么要解析 - 使用 BigInteger 库 https://rawgithub.com/silentmatt/javascript-biginteger/master/biginteger.js

    工作 jsFiddle 示例 - http://jsfiddle.net/svejdo1/sdV8L/

    var exponentB64 = 'ALVuSLbT';
    var exponentBytes = base64_decode(exponentB64);
    var result = new BigInteger(0);
    var multiplier = new BigInteger(1);
    for(var i = exponentBytes.length - 1; i >= 0; i--) {
        result = result.add(multiplier.multiply(exponentBytes[i]));
        multiplier = multiplier.multiply(256);
    }
    document.write(result.toString());
    
    
    function base64_decode(base64String) {
      var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
      var h1, h2, h3, h4, o1, o2, o3, bits, i = 0, bytes = [];
    
      do {
        h1 = b64.indexOf(base64String.charAt(i++));
        h2 = b64.indexOf(base64String.charAt(i++));
        h3 = b64.indexOf(base64String.charAt(i++));
        h4 = b64.indexOf(base64String.charAt(i++));
    
        bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
    
        o1 = bits >> 16 & 0xff;
        o2 = bits >> 8 & 0xff;
        o3 = bits & 0xff;
    
        bytes.push(o1);
        bytes.push(o2);
        bytes.push(o3);
      } while (i < base64String.length);
    
      return bytes;
    }
    

    【讨论】:

    • 哦,就这么简单? Let's use some real world example now: encoded string 'AK2/ss9tBm5mc4wThXztiHh5ApZDjkYA4//8vUtYtsCD6sUQ/GIoNwy7qIm4YIrr68c479nWXmIyhDVMuHZk4/Bque4xdPkNYMz5agOTweVa+NWuvWIivAZ34rHCzOVtM/Klkg/g7d0dIzYOkPfQmweyx1Bmdh2HVaKwmngvzb70d0cr7LN3YL6aDeg++w0d99INxEWMDb8s0VPVx5BHC0iOTS34JvB4vicxm2yfxzr6Y4cOJDfp1zoReogixp+zxljcMQ16dJly52tqBQH1lT8NWEtiAw8karYMRCBNKl9dn9d7FhopaAsJgZAHmZBuD6hi6IUZ6EHsUNm07xyU8S8=' expected decoded number 219337568015997254830809010245695848800620186109619871851429669559883715518752323465558437623481096892170... "Your message is too long by 461 characters ...”
    • 对不起——这是你的错;您的字符串包含不可见的空 Unicode 字符 8204 和 8203;你说字符串是base64,但不是。还有投票答案,它们为给定的数据提供了正确的输出——我什至在 C# 中使用 Microsoft System.Numeric.BigInteger 进行了测试,结果相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    相关资源
    最近更新 更多