How to use Node.js buffer module readUIntLE()
The Buffer.readUIntLE() method is used to read a specified number of bytes as an unsigned little-endian integer from an allocated buffer at a specified offset.
Endianness represents the order of bytes in a computer system. There are two types of endianness: little endian and big endian.
-
Big endian: Big endian stores the most significant bytes (MSB) first.
-
Little endian: Little endian stores the least significant bytes (LSB) first.
Prototype
buf.readUIntLE(offset, byteLength)
Parameters
offset: Determines the number of bytes to skip before starting to read.byteLength: Determines the number of bytes to read, which must be between and (inclusive).
Return value
Buffer.readUIntLE() returns an integer representation of the bytes read with up to a 48-bit accuracy.
Code
const buf = Buffer.from([0x05, 0x00, 0x00]);console.log(buf.readUIntLE(0, 3));
In the code above, we have an array of three hexadecimal values from which we create our buffer. Buffer.readUIntLE() starts reading from the offset and reads three bytes in little-endian format. Thus, it reads a hexadecimal value of 0x00 00 05 as an integer value of 5.
Free Resources