Skip to content
Related Articles

Related Articles

Improve Article

Node.js | Buffer.writeUInt32BE() Method

  • Last Updated : 20 Dec, 2019

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:






// 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);

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:




// 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);

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :