Node.js Buffer.writeUInt32BE() Method
Last Updated :
13 Oct, 2021
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:
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.
Return value: This parameter returns the sum of number of bytes written and the offset.
Example 1:
const buffer = Buffer.allocUnsafe(8);
console.log(buffer);
buffer.writeUInt32BE(0xabcdabcd, 0);
console.log(buffer);
buffer.writeUInt32BE(0xabcdabcd, 4);
console.log(buffer);
|
Output:
<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:
const buffer = Buffer.allocUnsafe(8);
console.log(buffer);
buffer.writeUInt32BE(0xabcdabcd, 5);
|
Output:
<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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...