Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

int
node.js
buffer
communitycreator

What is the Buffer.readUInt16BE() method in Node.js?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Buffer.readUInt16BE() method in Node.js reads 16 bits from a specified offset from a buffer in the Big Endianmost significant value is stored at the lowest storage address format.

Syntax

The Buffer.readUInt16BE() method can be declared as shown in the code snippet below:


Buffer.readUInt16BE(offset)

Parameter

  • offset: The offset determines how many bytes to skip before reading the buffer. In other words, it is the index of the buffer.

  • The offset value should be greater than -1 and less than (bufferLength - 1).
  • The default value of offset is 0.

Return value

The readUInt16BE() method returns an integer that is 16 bits read from the buffer of the given offset in Big Endian format.

Code

Consider the code snippet below, which demonstrates the use of the readUInt16BE() method:

const buff = Buffer.from([0x12, 0x34, 0x56, 0x78]);
console.log("buff stored in memory as", buff);
console.log("Reading at offset 0:", buff.readUInt16BE(0).toString(16));
console.log("Reading at offset 1:", buff.readUInt16BE(1).toString(16));
console.log("Reading at offset 2:", buff.readUInt16BE(2).toString(16));

Explanation

  • A buffer buff is declared in line 1.

  • The readUInt16BE() method is used in line 5 to get 16 bits from the index 0 in the big-endian format.

    As one index of the buffer is of 8 bits, we need to read 2 indices of the buffer. The readUInt16BE() returns two indices of buff starting from index 0 i.e. index = 0 and index + 1 = 1.

    As the format is big-endian, index 0 is printed before index 1.

  • The readUInt16BE() method is used in line 6 to get 16 bits from the index 1 in big-endian format.

    As one index of the buffer is of 8 bits, we need to read 2 indices of the buffer. The readUInt16BE() returns two indices of buff starting from index 1 i.e. index = 1 and index + 1 = 2.

    As the format is big-endian, index 1 is printed before index 2.

  • The readUInt16BE() method is used in line 7 to get 16 bits from the index 2 in big-endian format.

    As one index of the buffer is of 8 bits, we need to read 2 indices of the buffer. The readUInt16BE() returns two indices of buff starting from index 2 i.e. index = 2 and index + 1 = 3.

    As the format is big-endian, index 2 is printed before index 3.

RELATED TAGS

int
node.js
buffer
communitycreator

CONTRIBUTOR

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring