Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js
bytes
littleendian
communitycreator

What is the Buffer.writeUIntLE() method in Node.js?

Syed Muhammad Ali Mustafa

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.

The Buffer.writeUIntLE() method in Node.js writes a given number of bytes at a specified offset from a buffer in the Little Endianleast significant value is stored at the lowest storage address format.

Syntax

The Buffer.writeUIntLE() method can be declared as shown in the code snippet below:


Buffer.writeUIntLE(value, offset, byteCount)

Parameters

  • value: A 32 bit signed integer to be written in the buffer.

  • offset: The offset determines the number of bytes to skip before reading the buffer. In other words, it is the index of the buffer.

    • The value of offset should be range from 0 to (bufferLength - byteCount).
    • The default value of offset is 0.
  • byteCount: The number of bytes to be read.

    • The value of byteCount should range from 0 to 6.

Return value

The writeUIntLE() method returns an integer that offset + the number of bytes write to the buffer.

Code

The code snippet below demonstrates the use of the writeUIntLE() method:

const buff = Buffer.allocUnsafe(4);
buff.writeUIntLE(0x12345678, 0, 4);
console.log(buff);

Explanation

  • In line 1, we declare a buffer, buff.

  • The writeUIntLE() method is used in line 3 to write 4 bytes from the index 0 in the little-endian format.

    As one index of the buffer is 8 bits, we need to write at 4 indices. The writeUIntLE() method writes at 4 indices of buff, starting from the index 0 i.e., index = 0 - index + 4 = 4.

    Since the code is in the Little Endian format, index 4 is written before index 3, index 3 is written before index 2, and so on.

RELATED TAGS

node.js
bytes
littleendian
communitycreator

CONTRIBUTOR

Syed Muhammad Ali Mustafa

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