Skip to content

React-native bigint64 deserialization problem - refactoring/bigint64le #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
number to bigint refactoring and small changes
  • Loading branch information
Admdebian committed Nov 29, 2021
commit 78b1e2b1ce26e3e987bf81b057994ef59fa1eb38
15 changes: 4 additions & 11 deletions src/readBig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,18 @@ function boundsError(value: number, length: number) {
export function readBigUInt64LE(buffer: Buffer, offset = 0): bigint {
buffer = buffer.slice(offset)

const bufferLenght = Math.min(buffer.length, 8)

let tot: number = 0
const maxIndex = Math.log2(Number.MAX_VALUE)
for(let index = 0; index < bufferLenght; index++) {
let tot: bigint = BigInt(0)
for(let index = 0; index < buffer.length; index++) {
const value = buffer[index]
const exponent = 8*index

if(exponent > maxIndex)
throw new Error("out of range")

const addend = value * Math.pow(2, exponent)

const addend = BigInt(value * Math.pow(2, exponent))
tot += addend

// console.log("index: " + index + ", buffer value: " + value + ", decimal value: " + addend + ", tot: " + tot)
}

return BigInt(tot)
return tot
}

// This function works with react-native >= 0.66.1
Expand Down