What is the Buffer.readBigInt64LE() method in Node.js?

Overview

The Buffer.readBigInt64LE() method in Node.js is used to read a 64-bits large integer from a specified offset from a buffer in Little Endian formatwhere the least significant value is stored before the most significant value..

Syntax

The Buffer.readBigInt64LE() method can be declared as shown in the code snippet below:


Buffer.readBigInt64LE(offset)

Parameter

offset: The offset represents the number of bytes to skip before starting to read the buffer. In other words, it is the index of the buffer.


  • The offset value should be in the range 0 to (bufferLength-8).
  • The default value of offset is 0.

Return value

The Buffer.readBigInt64LE() method returns a 64-bit integer.

Code

Consider the code snippet below that shows how the Buffer.readBigInt64LE() method is used.

const buff = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
console.log("buff stored in memory as", buff);
console.log("Reading at offset 0:", buff.readBigInt64LE(0));

Output

buff stored in memory as <Buffer 00 00 00 00 ff ff ff ff>
Reading at offset 0: -4294967296

Explanation

  • line 1: We declare a buffer, buff.

  • line 5: We use the Buffer.readBigInt64LE() method to get 64-bits from the 0 index in the little-endian format.

An index of the buffer is of 8 bits. In order to get 64 bits, we need to read 64/8 = 8 indices of the buffer.

The Buffer.readBigInt64LE() reads 8 indices of buff in little-endian format(buff read as 0xffffffff00000000) and returns a 64-bit number.

Free Resources