Repository navigation
Buffer.from for the hex string #24491
Description
Activity
Works as expected. 'hex' encodes one byte as two hex characters - one character is not a valid hex string.
- addedinvalidIssues and PRs that are invalid.Issues and PRs that are invalid.
on Nov 19, 2018 @nlapshin To expand a bit on the answer here – the issue is that there’s no way to tell which byte(s) you mean by the hex string
5. It could be0x05, or0x50, or something else. Similarly, your example won’t work on numbers that lead to other odd hex string lengths – e.g.Buffer.from('abc', 'hex')will lose thec, because it doesn’t know what to do with it.If you need a way to convert numbers into
Buffers, maybe consider using theBuffer.writeInt*methods.If you have any more questions about this, https://github.com/nodejs/help might be a good place.
@nlapshin To expand a bit on the answer here – the issue is that there’s no way to tell which byte(s) you mean by the hex string
5. It could be0x05, or0x50, or something else. Similarly, your example won’t work on numbers that lead to other odd hex string lengths – e.g.Buffer.from('abc', 'hex')will lose thec, because it doesn’t know what to do with it.If you need a way to convert numbers into
Buffers, maybe consider using theBuffer.writeInt*methods.If you have any more questions about this, https://github.com/nodejs/help might be a good place.
Oh, thanks for the detailed response. I understood that hex string '5' or 'b' is one byte. And now, I understand, after your explantation, why 'Buffer.from' works that way.
Thanks a lot!
- added a commit that references this issue
on Jan 14, 2020 - added a commit that references this issue
on Jan 17, 2020 - added 2 commits that reference this issue
on Mar 14, 2020 Days since last bug caused by this: 0
Reacted by pcarette and Patrik ŠimuničReacted by KiritoReacted by WooMai@nodejs/buffer Is there any support for a breaking change that throws rather than discards data in this situation?
@nodejs/buffer Is there any support for a breaking change that throws rather than discards data in this situation?
And if not, how about a warning?
I'd be happy with a throw but we'd have to take the current behavior through a deprecation cycle first
@nlapshin To expand a bit on the answer here – the issue is that there’s no way to tell which byte(s) you mean by the hex string
5. It could be0x05, or0x50, or something else. Similarly, your example won’t work on numbers that lead to other odd hex string lengths – e.g.Buffer.from('abc', 'hex')will lose thec, because it doesn’t know what to do with it.
If you need a way to convert numbers intoBuffers, maybe consider using theBuffer.writeInt*methods.
If you have any more questions about this, https://github.com/nodejs/help might be a good place.Oh, thanks for the detailed response. I understood that hex string '5' or 'b' is one byte. And now, I understand, after your explantation, why 'Buffer.from' works that way.
Thanks a lot!
I use the conversion to the hexadecimal number buffer as follows
But, for number less that 16(15, 14 and so on) I get an empty buffer.
Example