You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Buffer.from(string, 'hex') needs a documentation update (was: Buffer.from(x, hex).toString(hex) does not return x for certain values of x) #29786
All of your problematic examples have an odd number of characters. Buffer.from() can't know how your hex-encoded string should be treated if it has an odd number of characters. Hex encoding means each byte is encoded as two hexadecimal characters. If you only provide one character instead, I imagine behavior is undefined, although I don't actually know. Surprisingly, I can't find mention of this in the documentation, so there may be a documentation pull request to be done here. A case can be made that this ought to throw an error but I wouldn't want to make that case before updating the docs.
People file issues like this from time to time which suggests that a doc update is in order. Here's a previous example: #24491
Reacted by Joren, Anna Henningsen and Nikita Skovoroda
changed the title [-]Buffer.from(x, `hex`).toString(`hex`) does not return x for certain values of x[/-][+]Buffer.from(string, 'hex') needs a documentation update (was: Buffer.from(x, `hex`).toString(`hex`) does not return x for certain values of x)[/+]on Oct 1, 2019
All of your problematic examples have an odd number of characters. Buffer.from() can't know how your hex-encoded string should be treated if it has an odd number of characters. Hex encoding means each byte is encoded as two hexadecimal characters. If you only provide one character instead, I imagine behavior is undefined, although I don't actually know. Surprisingly, I can't find mention of this in the documentation, so there may be a documentation pull request to be done here. A case can be made that this ought to throw an error but I wouldn't want to make that case before updating the docs.
People file issues like this from time to time which suggests that a doc update is in order. Here's a previous example: #24491
Then it is very inconsistent:
let n = 1000
console.log(n.toString(16)) => results in 3e8
console.log(Buffer.from(n.toString(16), 'hex')) => results in <Buffer 3e>
Then it is very inconsistent:
let n = 1000
console.log(n.toString(16)) => results in 3e8
console.log(Buffer.from(n.toString(16), 'hex')) => results in <Buffer 3e>
I suppose that suggests another possibility: add a leading 0 if needed so that '3e8' is treated like '03e8'. In addition to being a breaking change, that gets challenging for edge cases and may end up affecting performance. You wouldn't want to pad '3ez' but you would want to pad '3e8'. Things probably get weird fast there. @nodejs/buffer
Activity
Trott commentedon Oct 1, 2019
All of your problematic examples have an odd number of characters.
Buffer.from()can't know how your hex-encoded string should be treated if it has an odd number of characters. Hex encoding means each byte is encoded as two hexadecimal characters. If you only provide one character instead, I imagine behavior is undefined, although I don't actually know. Surprisingly, I can't find mention of this in the documentation, so there may be a documentation pull request to be done here. A case can be made that this ought to throw an error but I wouldn't want to make that case before updating the docs.People file issues like this from time to time which suggests that a doc update is in order. Here's a previous example: #24491
[-]Buffer.from(x, `hex`).toString(`hex`) does not return x for certain values of x[/-][+]Buffer.from(string, 'hex') needs a documentation update (was: Buffer.from(x, `hex`).toString(`hex`) does not return x for certain values of x)[/+]Trott commentedon Oct 1, 2019
Added
hacktoberfestandgood first issuelabels in case someone wants to try their hand at updating the relevant documentation.AdityaSrivast commentedon Oct 8, 2019
@Trott I would like to work on this issue
Trott commentedon Oct 9, 2019
Go for it.
doc: explain `hex` encoding in Buffer API
doc: explain `hex` encoding in Buffer API
doc: explain `hex` encoding in Buffer API
reneklootwijk commentedon Sep 25, 2020
Then it is very inconsistent:
let n = 1000
console.log(n.toString(16)) => results in 3e8
console.log(Buffer.from(n.toString(16), 'hex')) => results in <Buffer 3e>
Trott commentedon Sep 26, 2020
I suppose that suggests another possibility: add a leading 0 if needed so that
'3e8'is treated like'03e8'. In addition to being a breaking change, that gets challenging for edge cases and may end up affecting performance. You wouldn't want to pad'3ez'but you would want to pad'3e8'. Things probably get weird fast there. @nodejs/bufferdoc: explain `hex` encoding in Buffer API