GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — doc: improve `'hex'` Buffer decoding description and examples by gioragutt · Pull Request #41598 · nodejs/node Conversation
|
| may occur when decoding strings that do exclusively contain valid hexadecimal | ||
| characters. See below for an example. | ||
| may occur when decoding strings that do not exclusively contain valid | ||
| hexadecimal characters. See below for an example. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, it doesn't matter much if the strings exclusively contain hexadecimal characters. In fact, it might be more surprising that Buffer.from truncates data even when it only contains hex digits:
// only hex, still truncated :( Buffer.from('000', 'hex').toString('hex') === '00'
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is it because it reads 2 characters at a time?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Nvm, read the docs, which state:
Buffer.from('1a7g', 'hex'); // Prints <Buffer 1a>, data truncated when data ends in single digit ('7').
So the last '0' is truncated because it's a single character, and the hex decoder expects pairs of characters.
So since the third '0' does not have a pairing character, it is truncated.
Perhaps the description should state that? You know, not only in the code example?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| may occur when decoding strings that do not exclusively contain valid | ||
| hexadecimal characters. See below for an example. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| may occur when decoding strings that do not exclusively contain valid | |
| hexadecimal characters. See below for an example. | |
| may occur when decoding strings that do not exclusively consist of an even | |
| number of hexadecimal characters. See below for an example. |
Maybe something like this?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think that this, plus another code example (something like 123 -> 12) would be very nice
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@tniessen WDYT?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
SGTM. These are unfortunate oddities that we need to maintain for backward compatibility :(
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I took a better look at the examples and figured that I could achieve the same thing by altering the strings used in the examples.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| * `'hex'`: Encode each byte as two hexadecimal characters. Data truncation | ||
| may occur when decoding strings that do exclusively contain valid hexadecimal | ||
| characters. See below for an example. | ||
| may occur when decoding strings that do not exclusively consist of an even | ||
| number of hexadecimal characters. See below for an example. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Optional nit-pick suggestion: The Microsoft style guide we follow suggests might instead of may in this context. But I wonder if it's actually will occur rather than might occur? All told:
| * `'hex'`: Encode each byte as two hexadecimal characters. Data truncation | |
| may occur when decoding strings that do exclusively contain valid hexadecimal | |
| characters. See below for an example. | |
| may occur when decoding strings that do not exclusively consist of an even | |
| number of hexadecimal characters. See below for an example. | |
| * `'hex'`: Encode each byte as two hexadecimal characters. Data is truncated | |
| when decoding strings that do not exclusively consist of an even | |
| number of hexadecimal characters. See below for an example. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
'hex' Buffer decoding description'hex' Buffer decoding description and examples
|
Landed in dbe60a2 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
PR-URL: #41598 Fixes: #41594 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Buffer hex encoding description seems to omit a word
There was an error while loading. Please reload this page.