8000 doc: shorten character encoding introduction · nodejs/node@9f20534 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 9f20534

Browse files

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

doc/api/buffer.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,8 @@ changes:
149149
description: Removed the deprecated `raw` and `raws` encodings.
150150
-->
151151

152-
`Buffer` instances are commonly used to represent sequences of encoded characters
153-
such as UTF-8, UCS2, Base64, or even Hex-encoded data. It is possible to
154-
convert back and forth between `Buffer` instances and ordinary JavaScript strings
155-
by using an explicit character encoding.
152+
When string data is stored in or extracted out of a `Buffer` instance, a
153+
character encoding may be specified.
156154

157155
```js
158156
const buf = Buffer.from('hello world', 'ascii');
@@ -161,6 +159,11 @@ console.log(buf.toString('hex'));
161159
// Prints: 68656c6c6f20776f726c64
162160
console.log(buf.toString('base64'));
163161
// Prints: aGVsbG8gd29ybGQ=
162+
163+
console.log(Buffer.from('fhqwhgads', 'ascii'));
164+
// Prints: <Buffer 66 68 71 77 68 67 61 64 73>
165+
console.log(Buffer.from('fhqwhgads', 'ucs2'));
166+
// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>
164167
```
165168

166169
The character encodings currently supported by Node.js include:

0 commit comments

Comments
 (0)
0