Open In App

Node.js Buffer.readInt16LE() Method

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

The Buffer.readUInt16LE() method is an inbuilt application programming interface of class Buffer within the Buffer module which is used to read 16-bit value from an allocated buffer at a specified offset in Little Endian format.

Syntax:

Buffer.readUInt16LE(offset)

Parameters: This method accept single parameter offset which specifies the number of bytes to skip before read or simply signify the index in the buffer. The value of offset lies 0 <= offset <= Buffer.length – 2. Its default value is 0.

Return Value: This method returns an integer value that read from buffer in little endian format.

Below examples illustrate the use of Buffer.readUInt16LE() method in Node.js:

Example 1:




// Node program to demonstrate the  
// Buffer.readInt16LE() Method
   
// Allocating buffer from array
const buf = Buffer.from([0x21, 0x09, 0x19, 0x98]);
  
// Printing allocated buffer
console.log(buf);
   
// Reading 16bits data from the buffer
// and printing it as a string
console.log(buf.readUInt16LE(0).toString(16));
console.log(buf.readUInt16LE(1).toString(16));
console.log(buf.readUInt16LE(2).toString(16));


Output:

<Buffer 21 09 19 98>
921
1909
9819

Example 2:




// Node program to demonstrate the  
// Buffer.readInt16LE() Method
   
// Allocating buffer from array
const buf = Buffer.from([0x21, 0x09, 0x19, 0x98]);
  
// Printing allocated buffer
console.log(buf);
   
// Reading 16bits data from the buffer
// and printing it as a string
console.log(buf.readUInt16LE(0).toString(16));
console.log(buf.readUInt16BE(0).toString(16));
console.log(buf.readUInt16LE(1).toString(16));
console.log(buf.readUInt16BE(1).toString(16));
console.log(buf.readUInt16LE(2).toString(16));
console.log(buf.readUInt16BE(2).toString(16));


Output:

<Buffer 21 09 19 98>
921
2109
1909
919
9819
1998

Example 3:




// Node program to demonstrate the  
// Buffer.readInt16LE() Method
   
// Allocating buffer from array
const buf = Buffer.from([0x21, 0x09, 0x19, 0x98]);
  
// Printing allocated buffer
console.log(buf);
   
// Reading 16bits data from the buffer
// and printing it as a string
console.log(buf.readUInt16LE(0).toString(16));
console.log(buf.readUInt16LE(1).toString(16));
console.log(buf.readUInt16LE(2).toString(16));
   
// Wrong index is provoded to produce error
console.log(buf.readUInt16LE(3).toString(16));


Output:

<Buffer 21 09 19 98>
921
1909
9819
internal/buffer.js:49
  throw new ERR_OUT_OF_RANGE(type || 'offset',
  ^
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range.
It must be >= 0 and <= 2. Received 3
    at boundsError (internal/buffer.js:49:9)
    at Buffer.readUInt16LE (internal/buffer.js:128:5)
    . . .

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

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



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