📜  Node.js Buffer.readBigUInt64BE() 方法

📅  最后修改于: 2022-05-13 01:56:45.860000             🧑  作者: Mango

Node.js Buffer.readBigUInt64BE() 方法

Buffer.readBigUInt64BE() 方法用于从给定偏移量的缓冲区对象中读取无符号 64 位整数,并以 Big endian 形式返回结果。

句法:

buffer.readBigInt64BE( offset )

参数:此方法接受一个指定缓冲区对象位置的参数偏移量。它表示开始读取之前要跳过的字节数。 offset 的值在0 到 buffer.length – 8的范围内。默认值为 0。

返回值:此方法在 Big endian 的指定偏移量处读取一个无符号 64 位整数

示例 1:
文件名: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);


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);


使用以下命令运行index.js文件:

node index.js

输出:

Big Integer :- 15397457756159

示例 2:
文件名: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);

使用以下命令运行index.js文件:

node index.js

输出:

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'
}

上面的示例显示了错误,因为它的参数不在有效范围内。

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