What is the Buffer.writeBigUInt64LE() method Node.js?

The Buffer.writeBigUInt64LE() method

The Buffer.writeBigUInt64LE() method in Node.js is used to write 64-bits from a specified offset to a buffer in Little Endian formatthe least significant value is stored at the lowest storage address.

Syntax

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


Buffer.writeBigUInt64LE(value, offset)

Parameters

  • value: A 64-bit unsigned integer to be written in the buffer.

  • offset: The offset determines the number of bytes to skip before writing to the buffer.

In other words, it is the index of the buffer.

  • The offset value should be greater than -1 and less than (bufferLength - 7).
  • The default value of offset is 0.

Return value

The Buffer.writeBigUInt64LE() method returns an integer whose value is offset + the number of bytes written to the buffer.

Code

Consider the code snippet below, it demonstrates how to use the Buffer.writeBigUInt64LE() method:

const buff = Buffer.allocUnsafe(8);
buff.writeBigUInt64LE(0xabcdef1234567890n, 0);
console.log(buff);

Output

<Buffer 90 78 56 34 12 ef cd ab>

Explanation

  • In line 1, we declare a buffer, buff.

  • The writeBigUInt64LE() method is used in line 3 to write 64-bits from index 0 in the little-endian format.

    Since one index of the buffer is 8-bits, we need to write at 8 indices of buff.

  • The writeBigUInt64LE() method writes at 8 indices of buff starting from index 0, i.e., index = 0 - index + 8 - 1 = 7.

    Since the code is in the little Endian format, index 7 is written before index 6, index 6 is written before index 5, and so on.

Free Resources