buf.toString()

Decodes a buffer to a string using the given encoding.

Since Node 0.x Spec ↗

Syntax

buf.toString([encoding[, start[, end]]])

Parameters

NameTypeRequiredDescription
encoding string No Target encoding (default `'utf8'`); also `'hex'`, `'base64'`, `'latin1'`.
start number No Byte offset to start decoding.
end number No Byte offset to stop decoding.

Returns

string — The decoded string.

Examples

const buf = Buffer.from([0x68, 0x69]);
console.log(buf.toString('utf8'));
Output
hi
const buf = Buffer.from('secret');
console.log(buf.toString('base64'));
Output
c2VjcmV0

Notes

Decoding a slice that splits a multibyte UTF-8 character produces a replacement character; for chunked streams use `string_decoder` to handle boundaries correctly.

See also