簡體   English   中英

將大整數轉換為字節數組,反之亦然

[英]Convert big integer to byte array and vice versa

在javascript中,我對大整數進行計算,例如61597086133919319310755754988533408296568。我使用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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM