📜  Node.js Buffer.readUInt16LE() 方法

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

Node.js Buffer.readUInt16LE() 方法

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

句法:

Buffer.readUInt16LE( offset )

参数:此方法接受单个参数偏移量,它表示开始从缓冲区读取之前要跳过的字节数。偏移量可以在0 <= offset <= buf.length – 2范围内。偏移量的默认值为 0。

返回值:它以小端格式从指定偏移量返回一个整数。

下面的例子说明了在 Node.js 中使用Buffer.readUInt16LE()方法:

示例 1:

// Node.js program to demonstrate the 
// Buffer.readUInt16LE() method 
      
// Creating a buffer 
const buf = Buffer.from([0x7, 0x0,
       0x1, 0x1, 0x4, 0x5, 0x4, 0x6]); 
  
// Using Buffer.readUInt16LE() method
console.log(buf.readUInt16BE(0).toString(16));
  
// Using Buffer.readUInt16LE() method
console.log(buf.readUInt16BE(6).toString(16));
  
// Using Buffer.readUInt16LE() method
console.log(buf.readUInt16BE(2).toString(16));

输出:

700
406
101

示例 2:

// Node.js program to demonstrate the 
// Buffer.readUInt16LE() method 
      
// Creating a buffer 
const buf = Buffer.from([0x1714, 0x1024, 0x2113,
       0x2121, 0x1245, 0x1675, 0x1725, 0x1856]); 
  
// Using Buffer.readUInt16LE() method
console.log(buf.readUInt16BE(0).toString(16));
  
// Using Buffer.readUInt16LE() method
console.log(buf.readUInt16BE(6).toString(16));
  
// Using Buffer.readUInt16LE() method
console.log(buf.readUInt16BE(10).toString(16));

输出:

1424
2556
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out ofrange. 
It must be >= 0 and <= 6. Received 10    at boundsError (internal/buffer.js:49:9)
    at Buffer.readUInt16BE (internal/buffer.js:215:5)
    at /home/runner/index.js:14:17
    ......

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

注意:以上程序将使用node index.js命令编译运行。

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