Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
node.js
communitycreator

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

Saad Akbar

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.

Buffer.writeUInt8() is a built-in application programming interface of the Buffer class. This method is used to write values to a buffer at a pre-defined offset.

Syntax


Buffer.writeUInt8( value, offset )

Parameters

  • value: An unsigned 8-bit integer.

  • offset: The count of bytes to leave before writing onto the buffer.

Return value

writeUInt8() returns the buffer along with the values placed at the desired offset.

Code

In this example, we use writeUInt8() to add values to the buffer at a specific offset.

The buffer is then printed after writeUInt8() is called.

// buffer
const a = Buffer.allocUnsafe(4);
// Buffer.writeUInt8()
a.writeUInt8(0x10, 0);
// Print buffer
console.log(a);
// Buffer.writeUInt8()
a.writeUInt8(0x20, 1);
// Print buffer
console.log(a);
// Buffer.writeUInt8()
a.writeUInt8(0x30, 2);
// Print buffer
console.log(a)
// Buffer.writeUInt8()
a.writeUInt8(0x40, 3);
// Print buffer
console.log(a)

RELATED TAGS

javascript
node.js
communitycreator

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