Support for representing 64-bit integers in JavaScript
npm install int64-node±2^53. For projects requiring precise 64-bit integer handling, such as node-thrift, int64-node comes to the rescue.
bash
npm install int64-node
`
Usage
$3
`javascript
// Require the int64-node module
const Int64 = require('int64-node');
// Create Int64 instances
const int1 = new Int64(0x12345678, 0x9abcdef0);
const int2 = new Int64('123456789abcdef0');
`
$3
`javascript
// Perform arithmetic operations
const result1 = int1 + 1; // 4886718346
const result2 = int2 + 1; // Infinity
`
$3
`javascript
// Convert to different representations
const octetString1 = int1.toOctetString(); // '0000000123456789'
const octetString2 = int2.toOctetString(); // '123456789abcdef0'
const binaryString1 = int1.toString(2); // '100100011010001010110011110001001'
const binaryString2 = int2.toString(2); // 'Infinity'
`
$3
`javascript
// Check if the value is within integer-precise range
const isInt1Finite = isFinite(int1); // true
const isInt2Finite = isFinite(int2); // false
`
$3
`javascript
// Copy to a buffer
const buffer1 = int1.toBuffer(); //
const buffer2 = new Buffer(16);
int2.copy(buffer2, 0); // Copies the Int64 value to the buffer
`
$3
`javascript
// Create Int64 instances from a Buffer
const intFromBuffer = new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]));
// Create Int64 instances from a Buffer with an offset
const intFromOffset = new Int64(new Buffer([0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]), 4);
``