• Node.js Video Tutorials

Node.js - Buffer.readBigUInt64LE() Method



The Node.JS Buffer.readBigUInt64LE() method helps to read an unsigned little-endian 64-bit integer at a given offset from the current buffer object.

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 264-1.

Syntax

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

buf.readBigUInt64LE([offset])

Parameters

This method accepts a single parameter. The same is explained below.

  • offset − The offset indicates the position to start reading from the buffer. The offset will be between 0 to buffer.length-8. The default value is 0.

Return value

This method returns the 64-bit unsigned integer value of the contents of the current buffer at the given offset.

You can also make use of the alias function available: buf.readBigUint64LE(offset). Here the function has (I) is lower case.

Example

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

const buffer = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
console.log("buffer stored in memory as", buffer);  
console.log("Reading at big unsigned integer at offset 0:", buffer.readBigUInt64LE(0));

Output

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

buffer stored in memory as <Buffer 00 00 00 00 ff ff ff ff>
Reading at big unsigned integer at offset 0: 18446744069414584320n

Example

Let us create a buffer with 16 bits and see the value returned using the Node.JS Buffer.readBigUInt64LE() method.

const buffer = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff]);
console.log("Length of buffer is ", buffer.length);  
console.log("Reading at big integer at offset 2:", buffer.readBigUInt64LE(2));

Output

In the above example, the length of the buffer created is 16. So for offset, we can use the value from 0 to 8.

Length of buffer is  16
Reading at big integer at offset 2: 18446744073709486080n

Example

This example will check on the error if the offset is greater than the buffer.length -8. Let us create a buffer having a length of 8 and with that, you can offset the value as 0.

const buffer = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
console.log("buffer length is ", buffer.length);  
console.log("Reading at big integer at offset 1:", buffer.readBigUInt64LE(1));

Output

Since we have used an offset greater than 0 in the above program, it will throw an error as shown below −

buffer length is  8
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 <= 0. Received 1
   at boundsError (internal/buffer.js:83:9)
PS C:\nodejsProject> node src/testbuffer.js
buffer length is  8
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 <= 0. Received 1
   at boundsError (internal/buffer.js:83:9)
   at Buffer.readBigUInt64LE (internal/buffer.js:94:5)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:3:59)
   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)
   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
   at internal/main/run_main_module.js:17:47 {
   code: 'ERR_OUT_OF_RANGE'

Example

Let us also check the alias function buffer.readBigUint64LE() method.

const buffer = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
console.log("buffer length is ", buffer.length);  
console.log("Reading at big integer at offset 0:", buffer.readBigUint64LE(0));

Output

We have used the offset 0 to get the unsigned big integer. On executing the above program, it will generate the following output −

buffer length is 8
Reading at big integer at offset 0: 18446744069414584320n
nodejs_buffer_module.htm
Advertisements