From 6ed99a1ca4132e0921936925b2fe0818503f4154 Mon Sep 17 00:00:00 2001 From: Olan Byrne Date: Sat, 8 Oct 2016 15:46:52 +0100 Subject: [PATCH] doc: clarify buffer toString docs. Fixes: https://github.com/nodejs/node/issues/8971 PR-URL: https://github.com/nodejs/node/pull/8984 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- doc/api/buffer.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 42390b6d31ba00..df0a2a357d0836 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1811,12 +1811,13 @@ added: v0.1.90 --> * `encoding` {String} The character encoding to decode to. **Default:** `'utf8'` -* `start` {Integer} Where to start decoding. **Default:** `0` -* `end` {Integer} Where to stop decoding (not inclusive). **Default:** [`buf.length`] +* `start` {Integer} The byte offset to start decoding at. **Default:** `0` +* `end` {Integer} The byte offset to stop decoding at (not inclusive). + **Default:** [`buf.length`] * Return: {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`. +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`. Examples: @@ -1829,19 +1830,22 @@ for (var i = 0 ; i < 26 ; i++) { } // Prints: abcdefghijklmnopqrstuvwxyz -console.log(buf.toString('ascii')); +console.log(buf1.toString('ascii')); // Prints: abcde -console.log(buf.toString('ascii', 0, 5)); +console.log(buf1.toString('ascii', 0, 5)); const buf2 = Buffer.from('tést'); -// Prints: tés -console.log(buf.toString('utf8', 0, 3)); +// Prints: 74c3a97374 +console.log(buf2.toString('hex')); + +// Prints: té +console.log(buf2.toString('utf8', 0, 3)); -// Prints: tés -console.log(buf.toString(undefined, 0, 3)); +// Prints: té +console.log(buf2.toString(undefined, 0, 3)); ``` ### buf.toJSON()