Skip to main content
Buffer.toString - node__buffer.d.ts - Node documentation
method Buffer.toString

Usage in Deno

```typescript import { type Buffer } from "node:node__buffer.d.ts"; ```
Buffer.toString(
encoding?: BufferEncoding,
start?: number,
end?: number,
): string
Decodes `buf` to a string according to the specified character encoding in`encoding`. `start` and `end` may be passed to decode only a subset of `buf`. If `encoding` is `'utf8'` and a byte sequence in the input is not valid UTF-8, then each invalid byte is replaced with the replacement character `U+FFFD`. The maximum length of a string instance (in UTF-16 code units) is available as [constants.MAX_STRING_LENGTH](../.././node__buffer.d.ts/~/constants.MAX_STRING_LENGTH). ```js import { Buffer } from 'node:buffer'; const buf1 = Buffer.allocUnsafe(26); for (let i = 0; i < 26; i++) { // 97 is the decimal ASCII value for 'a'. buf1[i] = i + 97; } console.log(buf1.toString('utf8')); // Prints: abcdefghijklmnopqrstuvwxyz console.log(buf1.toString('utf8', 0, 5)); // Prints: abcde const buf2 = Buffer.from('tést'); console.log(buf2.toString('hex')); // Prints: 74c3a97374 console.log(buf2.toString('utf8', 0, 3)); // Prints: té console.log(buf2.toString(undefined, 0, 3)); // Prints: té ```

Parameters

optional
encoding: BufferEncoding = 'utf8'
The character encoding to use.
optional
start: number = 0
The byte offset to start decoding at.
optional
end: number = buf.length
The byte offset to stop decoding at (not inclusive).

Return Type

string