Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is the writeUIntLE function in Node.js?

Rukhshan Haroon

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 writeUIntLE function in Node.js comes with the Buffer module, and it is used to write an unsigned integerof maximum size 6 bytes in Little Endian format to a buffer at a specified offset.

In Little Endian format, the least significant bits are written first. For example, the 32-bit hex number AB CD AD BD would be represented as BD AD CD AB in Little Endian format.

Syntax

To use the writeUIntLE function, we will need a pre-allocated buffer. For this tutorial, we have named it mybuffer. The following syntax is used to use the writeUIntLE function:

mybuffer.writeUIntLE(value, offset, length);

Parameters

The writeUIntLE function takes in three parameters:

  • value: the unsigned integer that needs to be written to a pre-allocated buffer.
  • offset: the offset at or the number of bytes after which value will be written in the buffer. The minimum value of offset is 0, and its maximum value is the number length less than the value of mybuffer.len, which is the number of bytes in the buffer.

If the value of offset is out of the prescribed range, the ERR_OUT_OF_RANGE error is thrown. If length is greater than 6, the function exhibits undefined behavior.

  • length: size, in bytes, of value. Its value must be greater than 0 and smaller than or equal to 6.

Return value

The writeUIntLE function returns the sum of the offset and the number of bytes written to the buffer, upon successful execution.

Example

The program below allocates a buffer that is 8 bytes long. Subsequently, it uses the writeUIntLE function to write a 2 byte and 6 byte unsigned integer to mybuffer at specified offsets, then prints the state of mybuffer using the console.log function.

The program fills mybuffer with unsigned integers and prints its final state before terminating.

mybuffer = Buffer.alloc(8);
console.log(mybuffer);
mybuffer.writeUIntLE(0xabcd, 0, 2);
console.log(mybuffer);
mybuffer.writeUIntLE(0xdbacab125678, 2, 6);
console.log(mybuffer);

RELATED TAGS

node.js

CONTRIBUTOR

Rukhshan Haroon
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