Open In App

Node.js Buffer.writeUInt16LE() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The Buffer.writeUInt16LE() method is used to write specified bytes in Little Endian format to the buffer object. Here, value should be a valid unsigned 16-bit integer.

Syntax:

Buffer.writeUInt16LE( 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 to 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 and its default value is 0.

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

Example 1:




// Node.js program to demonstrate the
//Buffer.writeUInt16LE() Method
const buff = Buffer.allocUnsafe(4);
  
buff.writeUInt16LE(0xdead, 0);
console.log(buff);
  
buff.writeUInt16LE(0xbeef, 2)
console.log(buff);


Output:

<Buffer ad de 00 00>
<Buffer ad de ef be>

Example 2:




// Node.js program to demonstrate the
//Buffer.writeUInt16LE() Method
const buff = Buffer.allocUnsafe(4);
  
buff.writeUInt16LE(0xfeed, 0);
console.log(buff);
  
buff.writeUInt16LE(0xabcd, 2);
console.log(buff);


Output:

<Buffer ed fe 00 00>
<Buffer ed fe cd ab>
 

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

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


Last Updated : 13 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads