While testing conversion (Node.js driver) from/to Long and Uint8Array/BinInt I have noticed that there may be a discrepancy between the actual representation and the effect of Long.fromBytesBE
.
BigUint64Array represents its bytes as Big-Endian, meanwhile converting from the binary representation to Long one must use fromBytesLE
rather than fromBytesBE
as you would expect.
Here is a snippet of code:
const test = () => {
const value1 = BigInt(2);
const value2 = new Uint8Array(new BigUint64Array([value1]).buffer);
const fromBigInt = Long.fromBigInt(value1);
const fromBytesBE = Long.fromBytesBE(value2);
const fromBytesLE = Long.fromBytesLE(value2);
console.log({
value1,
value2,
fromBigInt,
fromBytesBE,
fromBytesLE
});
};
test();
/*
OUTPUT:
{
value1: 2n,
value2: Uint8Array(8) [2, 0, 0, 0, 0, 0, 0, 0],
fromBigInt: new Long("2"),
fromBytesBE: new Long("144115188075855872"),
fromBytesLE: new Long("2")
}
*/
Am I doing this wrong?
Kind regards,
Alexandru Comanescu