Skip to content

Commit

Permalink
Change bytes to num function
Browse files Browse the repository at this point in the history
  • Loading branch information
Duddino committed Dec 12, 2024
1 parent 7aadff5 commit d6137ee
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions scripts/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,11 @@ export function numToByteArray(num) {
* @returns {BigInt} converted number from bytes
*/
export function bytesToNum(bytes) {
if (bytes.length === 0) return 0n;
else return BigInt(bytes[0]) + 256n * bytesToNum(bytes.slice(1));
let result = 0n;
for (let i = 0n; i < BigInt(bytes.length); i++) {
result += BigInt(bytes[i]) * (256n ** i);
}
return result;
}

/**
Expand Down

0 comments on commit d6137ee

Please sign in to comment.