Node.js Buffer.writeUInt32BE() Method
Last Updated :
13 Oct, 2021
Improve
The Buffer.writeUInt32BE() method is used to write a number to an instance of the Buffer class. This value is written at the specified offset and in the big endian format.
Syntax:
javascript
Output:
javascript
Output:
buffer.writeUInt32BE(value, offset)Parameters: This method accept two parameters as mentioned above and described below:
- Value: This parameter holds the number to write. It should be a valid unsigned 32 bit integer. Also, behavior is not defined for invalid value.
- Offset: This parameter holds the number of bytes to skip. The value must be in range [0, buffer.length - 4]. It is optional parameter and the default value is zero.
// Node.js program to demonstrate the
// Buffer.writeUInt32BE method
// Creating a buffer of size 8
const buffer = Buffer.allocUnsafe(8);
console.log(buffer);
// Return value is 4
buffer.writeUInt32BE(0xabcdabcd, 0);
console.log(buffer);
// Return value is 8
buffer.writeUInt32BE(0xabcdabcd, 4);
console.log(buffer);
<Buffer 6c 69 63 65 00 00 00 00> <Buffer ab cd ab cd 00 00 00 00> <Buffer ab cd ab cd ab cd ab cd>Example 2:
// Node.js program to demonstrate the
// Buffer.writeUInt32BE method
// Creating a buffer of size 8
const buffer = Buffer.allocUnsafe(8);
console.log(buffer);
// Out of range error will be thrown
buffer.writeUInt32BE(0xabcdabcd, 5);
<Buffer b0 f1 67 fc 63 7f 00 00> Thrown: RangeError [ERR_OUT_OF_RANGE] ........Reference: https://nodejs.org/api/buffer.html#buffer_buf_readint32be_offset