📜  Node.js Buffer.readInt16BE() 方法

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

Node.js Buffer.readInt16BE() 方法

Buffer.readInt16BE() 方法是 Buffer 类的内置应用程序编程接口,带有 Buffer 模块,它以大端格式从指定偏移量的缓冲区中读取 16 位有符号整数。

句法:

Buffer.readInt16BE( offset )

参数:此方法接受单个参数偏移量,该偏移量指定在开始写入之前要跳过的字节数(整数)。 offset 的值在0 <= offset <= buf.length – 2范围内。它的默认值为零。

返回值:它返回偏移量以及写入的字节数。

示例 1:

// Node.js program to demonstrate the   
// Buffer.readInt16BE() method  
  
// Create a buffer 
const buf = Buffer.from([0, 3]);
  
// Display the result
console.log(buf.readInt16BE(0));

输出:

3

示例 2:

// Node.js program to demonstrate the   
// Buffer.readInt16BE() method  
  
// Create a buffer 
const buf = Buffer.from([0, 3]);
  
// Display the result
console.log(buf.readInt16BE(1));

输出:

internal/buffer.js:72
  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
. . .

参考: https://nodejs.org/api/buffer.html#buffer_buf_readint16be_offset