What is readInt32LE() in the Node.js Buffer Module?

The Buffer Module in Node.js provides access to Buffer objects that represent fixed-length sequences of bytes. readInt32LE() is a method defined in the Buffer Class that reads a 32-bit, little-endian integer from the Buffer object, starting from the specified offset. The prototype of readInt32LE() is as follows:

buf.readInt32LE([offset])

Parameters

offset: Number of bytes to skip before reading. offset must be an integer and lie in the range 0<= offset <= buf.length -4. The default value is 00.

Return value

A 32-bit, little-endian integer from buf, starting from the specified offset, is returned.

Note: The Buffer Class is accessible within the global scope. Therefore, you do not need to use the require('buffer').Buffer method to import the Buffer Module.

Example

const buf = Buffer.from([1, 62, -83, 4, 25]);
console.log(buf.readInt32LE(1));

Explanation

In the first line, we create the buffer object buf using Buffer.from(). Then, we display the 32-bit, little-endian integer from buf after skipping 1 byte by passing offset 1 to the buf.readInt32LE() method.

Copyright ©2024 Educative, Inc. All rights reserved