What is the writeUInt32BE function in Node.js?

The writeUInt32BE function in Node.js comes with the Buffer module, and it is used to write a 32-bit unsigned integer in Big Endian format to a buffer at a specified offset.

In Big Endian format, the least significant bits are written last. For example, the 32-bit hex number AB CD AD BD, where AB is the most significant byte, is in Big Endian format.

Syntax

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

mybuffer.writeUInt32BE(value, offset);

Parameters

The writeUInt32BE function takes in up to two parameters:

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

If offset is not specified, it is assumed to be 0 by default. If the value of offset is out of the prescribed range, the ERR_OUT_OF_RANGE error is thrown. If the value is greater than 32 bits, the function exhibits undefined behavior.

Return value

The writeUInt32BE 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 writeUInt32BE function to write a 32-bit unsigned integer to mybuffer at a specified offset, then prints the current state of mybuffer using the console.log function.

The program fills mybuffer with two 32-bit unsigned integers and prints its final state before terminating.

We can see that all numbers have been written in Big Endian format, as their byte order remains unchanged.

mybuffer = Buffer.alloc(8);
console.log(mybuffer);
mybuffer.writeUInt32BE(0xabcdadca, 0);
console.log(mybuffer);
mybuffer.writeUInt32BE(0xdbacab12, 4);
console.log(mybuffer);

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved