Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is the writeUIntBE 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 writeUIntBE function in Node.js comes with the Buffer module and is used to write an unsigned integer in the Big Endian format to a buffer at a specified offset. The maximum integer size here is 6 bytes.

In Big Endian format, the least significant bits are written last. In Little Endian format, this order is reversed. For example, the 32-bit hex number AB CD AD BD would be represented as BD AD CD AB in Little Endian format, and as AB CD AD BD in Big Endian format.

Syntax

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

mybuffer.writeUIntBE(value, offset, length);

Parameters

The writeUIntBE 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 writeUIntBE 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 writeUIntBE function to write a 2 byte and a 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.writeUIntBE(0xabcd, 0, 2);
console.log(mybuffer);
mybuffer.writeUIntBE(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