Open In App

Node.js Buffer.writeUInt16LE() Method

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:

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

Article Tags :