Skip to content
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

Buffer.from for the hex string #24491

Closed
nlapshin opened this issue Nov 19, 2018 · 7 comments
Closed

Buffer.from for the hex string #24491

nlapshin opened this issue Nov 19, 2018 · 7 comments
Labels
invalid Issues and PRs that are invalid.

Comments

@nlapshin
Copy link

I use the conversion to the hexadecimal number buffer as follows

   Buffer.from(number.toString(16), 'hex')

But, for number less that 16(15, 14 and so on) I get an empty buffer.

Example

   const buf = Buffer.from((5).toString(16), 'hex');
  
   console.log(buf); // <Buffer >
@seishun
Copy link
Contributor

seishun commented Nov 19, 2018

Works as expected. 'hex' encodes one byte as two hex characters - one character is not a valid hex string.

@seishun seishun closed this as completed Nov 19, 2018
@seishun seishun added the invalid Issues and PRs that are invalid. label Nov 19, 2018
@addaleax
Copy link
Member

@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 be 0x05, or 0x50, 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 the c, because it doesn’t know what to do with it.

If you need a way to convert numbers into Buffers, maybe consider using the Buffer.writeInt* methods.

If you have any more questions about this, https://github.com/nodejs/help might be a good place.

@nlapshin
Copy link
Author

@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 be 0x05, or 0x50, 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 the c, because it doesn’t know what to do with it.

If you need a way to convert numbers into Buffers, maybe consider using the Buffer.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!

HarshithaKP added a commit to HarshithaKP/node that referenced this issue Jan 14, 2020
Trott pushed a commit that referenced this issue Jan 17, 2020
fixes: #29786
refs: #29792
refs: #24491

PR-URL: #31352
Fixes: #29786
Refs: #29792
Refs: #24491
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
codebytere pushed a commit that referenced this issue Feb 17, 2020
fixes: #29786
refs: #29792
refs: #24491

PR-URL: #31352
Fixes: #29786
Refs: #29792
Refs: #24491
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
codebytere pushed a commit that referenced this issue Mar 14, 2020
fixes: #29786
refs: #29792
refs: #24491

PR-URL: #31352
Fixes: #29786
Refs: #29792
Refs: #24491
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
codebytere pushed a commit that referenced this issue Mar 17, 2020
fixes: #29786
refs: #29792
refs: #24491

PR-URL: #31352
Fixes: #29786
Refs: #29792
Refs: #24491
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
@hazae41
Copy link

hazae41 commented Jan 19, 2023

Days since last bug caused by this: 0

@Trott
Copy link
Member

Trott commented Jan 19, 2023

@nodejs/buffer Is there any support for a breaking change that throws rather than discards data in this situation?

@Trott
Copy link
Member

Trott commented Jan 19, 2023

@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?

@jasnell
Copy link
Member

jasnell commented Jan 19, 2023

I'd be happy with a throw but we'd have to take the current behavior through a deprecation cycle first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid.
Projects
None yet
Development

No branches or pull requests

6 participants