gpt4 book ai didi

javascript - 将大整数转换为字节数组,反之亦然

转载 作者:行者123 更新时间:2023-11-30 15:05:24 24 4
gpt4 key购买 nike

在 javascript 中,我对大整数进行计算,例如61597086133919310755754988533408296568。我使用 BigInteger 库对这类数字 (https://github.com/peterolson/BigInteger.js) 进行计算。从我的后端服务中,我收到这些数字作为长度为 16 的字节数组,我还必须将我的计算结果作为长度为 16 的字节数组发回。

您对如何正确解决这个问题有什么建议吗?

最佳答案

您可以通过将字节左移 8 位来减少字节数组,然后添加值。

var bytesArray = [255, 255, 255, 255],
intValue = bytesArray.reduce((r, a) => r.shiftLeft(8).add(a), bigInt.zero);

console.log(intValue.toString());

关于javascript - 将大整数转换为字节数组,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45826108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com