Skip to content

Commit e43ee37

Browse files
HarshithaKPTrott
authored andcommitted
doc: explain hex encoding in Buffer API
fixes: #29786 refs: #29792 refs: #24491 PR-URL: #31352 Fixes: #29786 Refs: #29792 Refs: #24491 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent cf5624c commit e43ee37

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

doc/api/buffer.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,20 @@ The character encodings currently supported by Node.js include:
203203

204204
* `'binary'`: Alias for `'latin1'`.
205205

206-
* `'hex'`: Encode each byte as two hexadecimal characters.
206+
* `'hex'`: Encode each byte as two hexadecimal characters. Data truncation
207+
may occur for unsanitized input. For example:
208+
209+
```js
210+
Buffer.from('1ag', 'hex');
211+
// Prints <Buffer 1a>, data truncated when first non-hexadecimal value
212+
// ('g') encountered.
213+
214+
Buffer.from('1a7g', 'hex');
215+
// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').
216+
217+
Buffer.from('1634', 'hex');
218+
// Prints <Buffer 16 34>, all data represented.
219+
```
207220

208221
Modern Web browsers follow the [WHATWG Encoding Standard][] which aliases
209222
both `'latin1'` and `'ISO-8859-1'` to `'win-1252'`. This means that while doing

0 commit comments

Comments
 (0)