Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

nodejs
communitycreator

What is the Node.js Buffer.toString() method?

Theodore Kelechukwu Onyejiaku

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.

In Node.js, the Buffer.toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type. Converting a buffer to a string is known as encoding, and converting a string to a buffer is known as decoding.

Syntax

Buffer.toString( encoding, start, end )

Parameters

  • encoding: This is the format in which the buffer data has to be encoded. The encodings supported by Node.js include utf8, utf16le, and latin1.

uft8 is the default if encoding is not specified.

  • start: This references the beginning data of the buffer. It starts from 0, meaning that the first data starts from 0.

The default value is 0.

  • end: This is the last index of the buffer data.

The default value is Buffer.length.

Example

In the example below, we create a Buffer with the Buffer.from() method. Then, we access the Buffer data and decode the Buffer.

// Create a Buffer
const ourBuffer = Buffer.from("edpresso");
// log out Buffer
console.log(ourBuffer);
// decode Buffer
console.log(ourBuffer.toString())

How to specify encoding

In the code above, we decode our buffer to a string. Note that we do not specify any encoding. Now, let’s specify some encoding.

// Create a Buffer
const ourBuffer = Buffer.from("edpresso");
// log out Buffer
console.log(ourBuffer);
// decode Buffer
console.log(ourBuffer.toString("utf8"))
console.log(ourBuffer.toString("utf16le"))
console.log(ourBuffer.toString("latin1"))

How to specify start and end parameters

As noted earlier, when the start and end parameters are not provided, the default 0 and Buffer.length are used as the start and end parameters. Hence, we have the complete data when we decode the buffer. Now, let’s specify start and end.

// Create a Buffer
const ourBuffer = Buffer.from("edpresso");
// log out Buffer
console.log(ourBuffer);
// decode Buffer but only first 3 data of Buffer
console.log(ourBuffer.toString("utf8", 0, 3))

RELATED TAGS

nodejs
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

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