-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: improve 'hex'
Buffer decoding description and examples
#41598
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -175,8 +175,8 @@ string into a `Buffer` as decoding. | |||||||||
encoding a `Buffer` to a string, this encoding will omit padding. | ||||||||||
|
||||||||||
* `'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 contain valid | ||||||||||
hexadecimal characters. See below for an example. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Maybe something like this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this, plus another code example (something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tniessen WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :( There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||
|
||||||||||
The following legacy character encodings are also supported: | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for 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 thatBuffer.from
truncates data even when it only contains hex digits:There was a problem hiding this comment.
Choose a reason for 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nvm, read the docs, which state:
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?