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.
The endianness of data refers to the order of bytes in the data. There are two types of Endianness:
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 complementBigInt
.
buf.writeBigInt64BE(value, offset)
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 . offset
must be between and buffer.length - 8
.buffer.writeBigInt64BE()
returns the offset plus the number of bytes written to the buffer.
The behavior of buffer.writeIntBE()
is undefined if the value
is anything other than a signed Integer
.
const b = BigInt(0x1213FFab5697);const buf = Buffer.alloc(16);buf.writeBigInt64BE(b, 0);console.log(buf);
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
and use writeBigInt64BE(b, 0)
to write our BigInt
value at offset
0. Then, we print the result as shown in the picture above.