int64-buffer 64-bit Integer Library

1.1.0 · active · verified Sun Apr 19

int64-buffer is a pure JavaScript library providing classes for reliable manipulation of 64-bit signed and unsigned integers (Int64BE, Uint64BE, Int64LE, Uint64LE). Unlike standard JavaScript numbers, which are IEEE-754 double-precision floats limited to 53 bits of integer precision, this package ensures full 64-bit accuracy. Currently at version 1.1.0, the library appears stable with infrequent but targeted updates, and has no notable release cadence. It stands out by explicitly *not* offering mathematical operations like addition or multiplication; instead, it focuses solely on efficient storage and conversion of 64-bit integers on `Buffer`, `Uint8Array`, or raw `Array` storage. It supports both big-endian (BE) and little-endian (LE) representations and operates without external dependencies, making it a lightweight (3KB minified) and portable solution for scenarios requiring precise 64-bit integer handling, such as parsing binary data streams or network protocols.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates instantiation of signed and unsigned 64-bit integers in both big-endian and little-endian formats, shows conversions, highlights JavaScript number precision limits, and illustrates writing a value into an existing buffer at an offset.

import { Int64BE, Uint64BE, Uint64LE } from 'int64-buffer';

// Working with a signed 64-bit integer (Big Endian)
const signedBig = new Int64BE(-1);
console.log(`Signed Int64BE: ${signedBig.toString(10)}`);
console.log(`Buffer representation: ${signedBig.toBuffer().toString('hex')}`);

// Working with an unsigned 64-bit integer from high/low 32-bit parts (Big Endian)
const unsignedBig = new Uint64BE(0x12345678, 0x9abcdef0);
console.log(`Unsigned Uint64BE (hex): ${unsignedBig.toString(16)}`);
console.log(`Unsigned Uint64BE (decimal string): ${unsignedBig.toString(10)}`);

// Demonstrating precision loss when coercing to standard JavaScript Number
const largeNumberJs = Math.pow(2, 63); // JavaScript Number, often loses precision
const largeUint64BE = new Uint64BE('9223372036854775808', 10); // Constructing from string for accuracy
console.log(`JS number (potential precision loss): ${largeNumberJs}`);
console.log(`Uint64BE (correct value): ${largeUint64BE.toString(10)}`);

// Creating from a byte array (Little Endian example)
const littleEndianBytes = new Uint8Array([0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]);
const littleEndianBig = new Uint64LE(littleEndianBytes);
console.log(`Uint64LE from bytes (hex): ${littleEndianBig.toString(16)}`);

// Example of writing a value into an existing buffer at an offset
const targetBuffer = Buffer.alloc(16); // Requires Node.js Buffer or a polyfill
const valueToWrite = new Uint64BE(0xCAFEBABE, 0xDEDDECAF);
new Uint64BE(targetBuffer, 8, valueToWrite.high, valueToWrite.low); // Write into buffer at offset 8
console.log(`Buffer with written value at offset 8: ${targetBuffer.toString('hex')}`);

view raw JSON →