Open In App

Node.js Buffer.readBigUInt64BE() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The Buffer.readBigUInt64BE() method is used to read unsigned 64 bit integer from a buffer object at a given offset and returns the result in Big endian. 

Syntax:

buffer.readBigUInt64BE( offset )

Parameters: This method accepts a single parameter offset which specifies the position of buffer object. It represents the number of bytes to skip before starting to read. The value of offset lies within the range 0 to buffer.length – 8. The default value is 0. 

Return value: This method reads an unsigned 64-bit integer at the specified offset in Big endian. 

Example 1: Filename: index.js 

javascript




// Node.js program to demonstrate the
// buffer.readBigUInt64BE() method
const buff = Buffer.from([0x00, 0x00,
    0x0e, 0x00, 0xff, 0xff, 0xff, 0xff]);
 
// Getting big integer value by
// using readBigUInt64BE method
const value = buff.readBigUInt64BE(0);
 
// Display the result
console.log("Big Integer :- " + value);


Run the index.js file using the following command:

node index.js

Output:

Big Integer :- 15397457756159

Example 2: Filename: index.js 

javascript




// Node.js program to demonstrate the
// buffer.readBigUInt64BE() method
const buff = Buffer.from([0x00, 0x00, 0x0e, 0x00, 0xff, 0xff, 0xff, 0xff]);
 
// Getting big integer value by
// using readBigUInt64BE method
const value = buff.readBigUInt64BE(1);
 
// Display the result
console.log("Big Integer :- " + value);


Run the index.js file using the following command:

node index.js

Output:

internal/buffer.js:77
  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:77:9)
    at Buffer.readBigUInt64BE (internal/buffer.js:108:5)
    at Object. (F:\java\GFG.js:7:20)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'ERR_OUT_OF_RANGE'
}

The above example shows the error because its parameters are not in the valid range. 

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/buffer.html#buffer_buf_readbiguint64be_offset



Last Updated : 07 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads