Node.js | Buffer.readInt16BE() Method

The Buffer.readInt16BE() Method is an inbuilt application programming interface of class Buffer with in Buffer module which reads a 16-bit signed integer from the buffer at the specified offset in the big endian format.

Syntax:

Buffer.readInt16BE( offset )

Parameters: This method accepts a single parameter offset which specifies the number (integer) of bytes to skip before starting to write. The value of offset lies within the range 0 <= offset <= buf.length – 2. Its default value is zero.

Return value: It returns the offset along with number of bytes written.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// Buffer.readInt16BE() method  
  
// Create a buffer 
const buf = Buffer.from([0, 3]);
  
// Display the result
console.log(buf.readInt16BE(0));

chevron_right


Output:

3

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// Buffer.readInt16BE() method  
  
// Create a buffer 
const buf = Buffer.from([0, 3]);
  
// Display the result
console.log(buf.readInt16BE(1));

chevron_right


Output:

internal/buffer.js:72
  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 <= 0. Received 1
. . .

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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.