Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

What is readIntLE() Buffer module in Node.js?

Adnan Abbas

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Buffer objects are used to represent a sequence of bytes of pre-determined size. They are similar to arrays but cannot be resized. Since the Buffer class is designed to handle raw binary data, it provides various methods, particularly for binary data.

The readIntLE method is used to read a specific number of bytes at an offset provided in the function’s arguments. The bytes read from the buffer are represented as a big-endian, two’s complement signed integer value supporting up to 48 bits of accuracy.

Syntax and parameters

The method can be accessed using the . notation on a buffer object. In the following example, buf is a Buffer object.

buf.readIntLE(offset, byteLength)
  • offset is the number of bytes to skip before reading the buffer. It is initialized with 0 by default. The offset must be between 0 and (buf.length - byteLength).

buf.length is the size of the buffer, i.e., the number of elements in the buffer.

  • byteLength is the number of bytes to be read. This number should be between 0 and 6.

Return value

The function returns a little-endian, two’s complement signed integer value.

Example

We will demonstrate how to use the readIntLE in the following example.

const buf = Buffer.from([0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80]);
console.log(buf.readIntLE(0, 6).toString(16));
console.log(buf.readIntLE(1, 6).toString(16));
//console.log(buf.readIntLE(3, 6).toString(16));
readIntLE in Node
  • We first initialize the buffer using an array of hexadecimal numbers and the .from method of the Buffer object.
  • In line 3, the offset is set as 0, and the bytes to be read are set as 6. The first 6 values are printed in the reverse order, as little-endian is an order in which the least significant value is stored first.
  • In line 4, we provide an offset of 1. The first value is skipped, and the next 6 values are printed in the little-endian order.
  • You can comment out line 5 to check that the compiler throws an ERR_OUT_OF_RANGE (out of range) error when the offset is greater than the limit we described above in the heading “Syntax and parameters.”

The toString method is used to convert the result into text. We provide 16 as the radix argument of the function to convert the result into a hexadecimal representation.

RELATED TAGS

CONTRIBUTOR

Adnan Abbas
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring