64-bit Integer Representation for JavaScript

0.4.0 · abandoned · verified Sun Apr 19

The `node-int64` library provides a class, `Int64`, to precisely represent 64-bit integers in JavaScript, addressing the inherent limitation of JavaScript's IEEE 754 double-precision floats which lose integer precision beyond +/- 2^53. It is currently at version 0.4.0, which was published 11 years ago, and the project is not actively maintained, with its author noting that native `BigInt`s will likely obsolete it. The library aims to provide a Number-like object primarily for carrying 64-bit integer values, such as those encountered in binary data formats like Apache Thrift, rather than for performing 64-bit integer arithmetic. While instances can be coerced to JavaScript numbers for basic operations, values exceeding the safe integer range will resolve to `Infinity`. Its primary utility lies in preserving the exact 64-bit binary representation, which can be extracted as an octet string or Node.js `Buffer`. This makes it suitable for interoperability with systems requiring precise 64-bit data types, distinguishing it from libraries focused on arbitrary-precision arithmetic.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating `Int64` instances from various inputs (number, hex string, Buffer) and basic output operations like `toOctetString` and `toBuffer`.

const Int64 = require('node-int64');

// Create an Int64 from a JavaScript number (up to 2^53 precision)
const smallInt = new Int64(0x123456789);
console.log(`Small Int: ${smallInt.toString(16)} (value: ${smallInt.valueOf()})`);

// Create an Int64 from a hexadecimal string for full 64-bit precision
const bigHex = '123456789abcdef0';
const largeInt = new Int64(bigHex);
console.log(`Large Int (hex): ${largeInt.toOctetString()} (value: ${largeInt.valueOf()})`);

// Create an Int64 from a Node.js Buffer
const buffer = Buffer.from([0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x02, 0x03, 0x04]);
const intFromBuffer = new Int64(buffer);
console.log(`From Buffer: ${intFromBuffer.toOctetString()} (value: ${intFromBuffer.valueOf()})`);

// Extract the 64-bit value into a Buffer
const outputBuffer = intFromBuffer.toBuffer();
console.log(`To Buffer: ${outputBuffer.toString('hex')}`);

// Check if the internal value is within JS safe integer range
console.log(`Is smallInt finite (JS precision): ${isFinite(smallInt)}`);
console.log(`Is largeInt finite (JS precision): ${isFinite(largeInt)}`);

view raw JSON →