Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js
communitycreator

What is the Buffer.readIntBE() method 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.readIntBE() method in Node.js reads a given number of bytes at a specified offset from a buffer in the Big Endianmost significant value is stored at the lowest storage address format.

Syntax

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


Buffer.readIntBE(offset, byteCount)

Parameter

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

  • byteCount: The number of bytes to be read.


  • The value of offset should be in the range 0 to (bufferLength - byteCount).
  • The default value of offset is 0.
  • The value of byteCount should be in the range 0 to 6.

Return value

The readIntBE() method returns an integer that is the byteCount number of bytes read from the buffer of the given offset in Big Endian format.

Code

The code snippet below demonstrates the readIntBE() method:

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

Explanation

  • A buffer buff is declared in line 1.

  • The readIntBE() method is used in line 5 to get 2 bytes from the index 0 in the big-endian format. As one index of the buffer is 1 byte, we need to read 2 indices.

    The readIntBE() method 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 readIntBE() method is used in line 6 to get 4 bytes from the index 0 in big-endian format.

    As one index of the buffer is 1 byte, we need to read 4 indices of the buffer. The readIntBE() method returns four indices of buff starting from index 0 i.e. index = 0, index + 1 = 1, index + 2 = 2, and index + 3 = 3.

    As the format is big-endian, the indices are printed in order (index 0, index 1, index 2, index 3).

RELATED TAGS

node.js
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