Skip to content

Commit 70ff15d

Browse files
committed
doc: explain hex encoding in Buffer API
fixes: nodejs#29786 refs: nodejs#29792 refs: nodejs#24491
1 parent 9530362 commit 70ff15d

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

doc/api/buffer.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,23 @@ 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-hexa-decimal 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> , fully qualified hexadecimal data
219+
220+
Buffer.from((163).toString(16), 'hex');
221+
// Prints <Buffer a3> , sanitized hexadecimal data
222+
```
207223

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

0 commit comments

Comments
 (0)