int64-buffer
64bit Long Integer on Buffer/Array/ArrayBuffer in Pure JavaScript
JavaScript's number type, based on IEEE-754, can only handle 53 bits of precision.
This module provides two pairs of classes: Int64BE
/Uint64BE
and Int64LE
/Uint64LE
, which can hold 64-bit long integers without losing any bits.
Features
Int64BE
/Int64LE
for signed integers, Uint64BE
/Uint64LE
for unsigned integers.Int64BE
/Uint64BE
for big-endian, Int64LE
/Uint64LE
for little-endian.Buffer
/Uint8Array
/Array
/Array
-like storage of 8 bytes length with offset.- No mathematical methods provided, such as
add()
, sub()
, mul()
, div()
, etc. - Optimized only for 64 bits. If you need Int128, use bignum or similar libraries.
- Small. 3KB when minified. No other modules required. Portable pure JavaScript.
- Tested on node.js v18, v20, v22 and Web browsers.
Usage
Int64BE
is the class to host a 64-bit signed long integer int64_t
.
import {Int64BE} from "int64-buffer";
const big = new Int64BE(-1);
console.log(big - 0);
console.log(big.toBuffer());
It uses Buffer
on Node.js and Uint8Array
on modern Web browsers.
Uint64BE
is the class to host a 64-bit unsigned positive long integer uint64_t
.
import {Uint64BE} from "int64-buffer";
const big = new Uint64BE(Math.pow(2, 63));
console.log(big - 0);
console.log(big + "");
Int64LE
and Uint64LE
work the same way as above but with little-endian storage.
Input Constructor
const big = new Uint64BE(1234567890);
console.log(big - 0);
const big = new Uint64BE(0x12345678, 0x9abcdef0);
console.log(big.toString(16));
- new Uint64BE(string, radix)
const big = new Uint64BE("123456789abcdef0", 16);
console.log(big.toString(16));
const buffer = Buffer.from([1,2,3,4,5,6,7,8]);
const big = new Uint64BE(buffer);
console.log(big.toString(16));
const uint8array = new Uint8Array([1,2,3,4,5,6,7,8]);
const big = new Uint64BE(uint8array);
console.log(big.toString(16));
- new Uint64BE(arraybuffer)
const arraybuffer = (new Uint8Array([1,2,3,4,5,6,7,8])).buffer;
const big = new Uint64BE(arraybuffer);
console.log(big.toString(16));
const array = [1,2,3,4,5,6,7,8];
const big = new Uint64BE(array);
console.log(big.toString(16));
- new Uint64BE(buffer, offset)
const buffer = Buffer.from([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
const big = new Uint64BE(buffer, 8);
console.log(big.toString(16));
- new Uint64BE(buffer, offset, number)
const buffer = Buffer.from(16);
const big = new Uint64BE(buffer, 8, 0x1234567890);
console.log(big.toString(16));
console.log(buffer[15].toString(16));
- new Uint64BE(buffer, offset, high, low)
const buffer = new Uint8Array(16);
const big = new Uint64BE(buffer, 8, 0x12345678, 0x9abcdef0);
console.log(big.toString(16));
console.log(buffer[15].toString(16));
- new Uint64BE(buffer, offset, string, radix)
const buffer = new Array(16);
const big = new Uint64BE(buffer, 8, "123456789abcdef0", 16);
console.log(big.toString(16));
console.log(buffer[15].toString(16));
Output Methods
const big = new Uint64BE(1234567890);
console.log(big - 0);
const big = new Uint64BE(1234567890);
console.log(big.toNumber());
const big = new Uint64BE(0x1234567890);
console.log(big.toString());
console.log(big.toString(16));
const big = new Uint64BE([1,2,3,4,5,6,7,8]);
console.log(big.toBuffer());
const big = new Uint64BE(0);
const buf = new Int8Array(big.toArrayBuffer());
console.log(buf);
const big = new Uint64BE([1,2,3,4,5,6,7,8]);
console.log(big.toArray());
Browsers Build
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="https://cdn.jsdelivr.net/npm/int64-buffer/dist/int64-buffer.min.js"></script>
<script>
const i = new Int64BE("1234567890123456789");
console.log(i.toString(10));
const u = new Uint64BE([0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
console.log(u.toString(16));
</script>
Links
The MIT License (MIT)
Copyright (c) 2015-2024 Yusuke Kawasaki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.