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

The Buffer.writeBigInt64BE() method in Node.js is used to write a BigInt to the buffer as a signed integer. Buffer.writeBigInt64BE() writes to the buffer in the Big Endian format.

Endianness

The endianness of data refers to the order of bytes in the data. There are two types of Endianness:

  1. Little Endian
  2. Big Endian

Little Endian stores the Least Significant Byte (LSB) first and is commonly used in Intel processors.

Big Endian stores the Most Significant Byte (MSB) first.

Big Endian is also called ‘Network Byte Order’ because most networking standards expect the MSB first.

Note: The value is interpreted as a two’s complement BigInt.

Syntax

buf.writeBigInt64BE(value, offset)

Parameters

  • value: The value of the unsigned integer to be written to the buffer.
  • offset: The offset from the starting position where the value will be written. The default value for offset is 00. offset must be between 00 and buffer.length - 8.

Return value

buffer.writeBigInt64BE() returns the offset plus the number of bytes written to the buffer.

Exceptions

The behavior of buffer.writeIntBE() is undefined if the value is anything other than a signed Integer.

Example

const b = BigInt(0x1213FFab5697);
const buf = Buffer.alloc(16);
buf.writeBigInt64BE(b, 0);
console.log(buf);

Output

widget

To demonstrate the use of writeBigInt64BE(), we create a BigInt and name it b:

const b = BigInt(0x1213FFab5697);

We allocate a buffer, buf, of size 1616 and use writeBigInt64BE(b, 0) to write our BigInt value at offset 0. Then, we print the result as shown in the picture above.

Free Resources