Open In App
Related Articles

Node.js Buffer.writeUInt16BE() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

The Buffer.writeUInt16BE() method is used to write specified bytes using Big endian format to the buffer object. The value should be a valid unsigned 16-bit integer.

Syntax:

Buffer.writeUInt16BE( value, offset )

Parameters: This method accept two parameters as mentioned above and described below:

  • value: It is an integer value and that is to be written into the buffer.
  • offset: It is an integer value and it represents the number of bytes to skip before starting to write and the value of offset lies within the range 0 to buffer.length – 2. Its default value is 0.

Return value: It returns an integer value offset plus number of bytes written.

Example 1:




// Node.js program to demonstrate the  
// Buffer.writeUInt16BE() Method
  
// Allocate a buffer
const buf = Buffer.allocUnsafe(4);
  
// Write the buffer element in BE format
buf.writeUInt16BE(0xabcd, 0);
  
// Display the buffer list
console.log(buf);
  
// Write the buffer element in BE format
buf.writeUInt16BE(0xfede, 2)
  
// Display the buffer list
console.log(buf);


Output:

<Buffer ab cd f4 09>
<Buffer ab cd fe de>

Example 2:




// Node.js program to demonstrate the  
// Buffer.writeUInt16BE() Method
  
// Allocate a buffer
const buf = Buffer.allocUnsafe(4);
  
// Write the buffer element in BE format
buf.writeUInt16BE(0xabab, 0);
  
// Display the buffer list
console.log(buf);
  
// Write the buffer element in BE format
buf.writeUInt16BE(0xefde, 2);
  
// Display the buffer list
console.log(buf);


Output:

<Buffer ab ab ad 09>
<Buffer ab ab ef de>

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/buffer.html#buffer_buf_writeuint16be_value_offset


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 13 Oct, 2021
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials