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 readInt32LE
method is used to read a signed, little-endian 32-bit integer at an offset provided in the function’s arguments. The bytes read from the buffer are represented as a two’s complement signed integer value.
The method can be accessed using the .
notation on a buffer object. In the following example, buf
is a Buffer
object.
buf.readInt32LE([offset])
0
and (buf.length - 4)
.
buf.length
is the size of the buffer, i.e., the number of elements in the buffer.
The function returns a two’s complement signed integer.
We will demonstrate how to use the readInt32LE
in the following example:
const buf = Buffer.from([0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80]);console.log(buf.readInt32LE(0).toString(16));console.log(buf.readInt32LE(1).toString(16));//console.log(buf.readInt32LE(5).toString(16));
.from
method of the Buffer
object.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 converts the result into text. We provide 16 as the
radix
argument of the function to convert the result into a hexadecimal representation.