• Node.js Video Tutorials

Node.js - Buffer.readBigInt64BE() Method



The NodeJS Buffer.readBigInt64BE() method helps to read a signed big endian 64-bit integer at a given offset from a buffer object.

  • The signed 64-bit integer is 264-1. The result will be a 20-digit number.

  • The minimum value that can be stored in 64-bit is: -9,223,372,036,854,775,808.

  • The maximum value that can be stored in 64-bit is: 9,223,372,036,854,775,807.

In the Big endian system, it stores the most significant byte at the lowest storage address. And Little endian is a system in which the least significant byte is at the highest storage address.

Please note this method is available from node version 12.0.0 onwards.

Syntax

Following is the syntax of the NodeJS readBigInt64BE() method −

buf.readBigInt64LE(offset)

Parameters

The method buf.readBigInt64LE() takes in a single parameter. The same is explained below.

  • offset − The offset that indicates the position to start reading. The offset greater than or equal to 0 and also less than or equal to buffer.length-8. The default value is 0.

Return value

The method buf.readBigInt64LE() returns the 64-bit signed integer value at the offset given.

Example

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

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 integer at offset 0:", buffer.readBigInt64BE(0));

Output

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

Example

Let us create a buffer with 16 bits and see the value returned using readBigInt64BE() 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 0:", buffer.readBigInt64BE(2));

Above the length of the buffer created is 16. So for offset we can use the value from 0 to 8.

Output

Length of buffer is  16
Reading at big integer at offset 0: 281474976710655n

Example

In this example will check on the error if offset is greater than buffer.length -8. Let us create a buffer having length 8 and with that you can offset value as 0. Using an offset greater than 0 will throw an error as shown below.

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.readBigInt64BE(1));

Output

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.readBigInt64BE (internal/buffer.js:152:5)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:5: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's
nodejs_buffer_module.htm
Advertisements