Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js
bigendian
communitycreator

What is the Buffer.writeInt32BE() 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 writeInt32BE() method in Node.js writes a 32 bits integer at a specified offset from a buffer in the Big Endianmost significant value is stored at the lowest storage address format.

Syntax


Buffer.writeInt32BE(value, offset)

Parameters

  • value: A 32 bit signed integer to be written in the buffer.

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

    • The value of offset should range from 0 to (bufferLength - 4).
    • The default value of offset is 0.

Return value

The writeInt32BE() method returns an integer whose value is the offset and the number of bytes written to the buffer.

Code

The code snippet below demonstrates the use of the writeInt32BE() method.

const buff = Buffer.allocUnsafe(4);
buff.writeInt32BE(0x12345678, 0);
console.log(buff);

Explanation

  • In line 1, we declare a buffer, buff.

  • The writeInt32BE() method is used in line 3 to write 32 bits from the index 0 in the big-endian format.

    As one index of the buffer is 8 bits, we need to write at 4 indices of the buffer.

    The writeInt32BE() method writes at 4 indices of buff starting from index 0 i.e., index = 0 - index + 4 - 1 = 3.

Since the snippet is in the Big Endian format, index 0 is written before index 1, index 1 is written before index 2, and so on.

RELATED TAGS

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