• Node.js Video Tutorials

Node.js - Buffer.writeBigUInt64LE() Method



The NodeJS Buffer.writeBigInt64LE() method helps in writing an unsigned 64-bit integer at a given offset to the buffer in little endian form.

In computer programming unsigned integers refers to a positive integer and signed integer refers to positive and negative values.

The minimum value with unsigned integers for 64-bit is 0 and maximum value is $2^{64}$-1.

Syntax

Following is the syntax of the Node.JS Buffer.writeBigUInt64LE() Method

buf.writeBigUInt64LE(value[, offset])

Parameters

This method accepts two parameters. The same is explained below.

  • value − (required) Unsigned 64-bit integer to be written to the buffer.

  • offset − (optional) The offset here indicates the position to start writing. The offset has to be greater than or equal to 0 and less than or equal to buffer.length-8. The default value is 0.

Return value

The method buf.writeBigUInt64LE() writes the 64-bit unsigned integer value and returns offset plus the number of bytes written.

You can also make use of the alias function available: buf.writeBigUint64LE(value[,offset]). Here the function has (I) is lower case.

Example

To create a buffer, we are going to make use of NodeJS Buffer.alloc() method −

const buffer = Buffer.alloc(10);
buffer.writeBigUInt64LE(0x0108890060708n, 0);
console.log(buffer);

Output

The offset we have used with this method is 0. On execution the 64-bit unsigned integer at the 0th position will be written to the buffer created. The buffer length created for the above is 10. So we can only use the offset with values from 0 to 2. If any value >2 it will give an error ERR_OUT_OF_RANGE.

<Buffer 08 07 06 90 88 10 00 00 00 00>

Example

In this example will use an offset that is greater than buffer.length - 8. It should throw an error as shown below.

const buffer = Buffer.alloc(10);
buffer.writeBigUInt64LE(0x0108890060708n, 3);
console.log(buffer);

Output

internal/buffer.js:83
   throw new ERR_OUT_OF_RANGE(type || 'offset',
   ^
   
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 2. Received 3
   at boundsError (internal/buffer.js:83:9)
   at checkBounds (internal/buffer.js:52:5)
PS C:\nodejsProject> node src/testbuffer.js
internal/buffer.js:83
   throw new ERR_OUT_OF_RANGE(type || 'offset',
   ^
   
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 2. Received 3
   at boundsError (internal/buffer.js:83:9)
   at checkBounds (internal/buffer.js:52:5)
   at checkInt (internal/buffer.js:71:3)
   at writeBigU_Int64LE (internal/buffer.js:576:3)
   at Buffer.writeBigUInt64LE (internal/buffer.js:598:10)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:2:8)
   at Module._compile (internal/modules/cjs/loader.js:1063:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
   at Module.load (internal/modules/cjs/loader.js:928:32)
   at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
      code: 'ERR_OUT_OF_RANGE'
   }

Example

To create a buffer, we are going to make use of Buffer.allocUnsafe() method −

const buffer = Buffer.allocUnsafe(10);
buffer.writeBigUInt64LE(0x010060708n);
console.log(buffer);

Output

The offset is not used, by default it will be taken as 0. So the output for above is as shown below −

<Buffer 08 07 06 10 00 00 00 00 00 00>
nodejs_buffer_module.htm
Advertisements