buf.readIntLE(offset, byteLength)


  • offset <integer> 开始读取的偏移量。必须满足0 <= offset <= buf.length - byteLength
  • byteLength <integer> 要读取的字节数。必须满足0 < byteLength <= 6
  • 返回: <integer>

buf 中指定的 offset 读取 byteLength 个字节,并将读取的值解析为二进制补码值。 最高支持 48 位精度。

const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);

console.log(buf.readIntLE(0, 6).toString(16));
// 输出: -546f87a9cbee
console.log(buf.readIntBE(0, 6).toString(16));
// 输出: 1234567890ab
console.log(buf.readIntBE(1, 6).toString(16));
// 抛出异常 ERR_INDEX_OUT_OF_RANGE。
console.log(buf.readIntBE(1, 0).toString(16));
// 抛出异常 Throws ERR_OUT_OF_RANGE。
  • offset <integer> Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength.
  • byteLength <integer> Number of bytes to read. Must satisfy 0 < byteLength <= 6.
  • Returns: <integer>

Reads byteLength number of bytes from buf at the specified offset and interprets the result as a two's complement signed value. Supports up to 48 bits of accuracy.

const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);

console.log(buf.readIntLE(0, 6).toString(16));
// Prints: -546f87a9cbee
console.log(buf.readIntBE(0, 6).toString(16));
// Prints: 1234567890ab
console.log(buf.readIntBE(1, 6).toString(16));
// Throws ERR_INDEX_OUT_OF_RANGE
console.log(buf.readIntBE(1, 0).toString(16));
// Throws ERR_OUT_OF_RANGE