Skip to content

Buffer.from for the hex string #24491

Description

@nlapshin

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 >

Activity

  1. seishun commented on Nov 19, 2018

    @seishun
    Contributor

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

  2. addaleax commented on Nov 19, 2018

    @addaleax
    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.

  3. nlapshin commented on Nov 20, 2018

    @nlapshin
    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!

  4. hazae41 commented on Jan 19, 2023

    @hazae41

    Days since last bug caused by this: 0

  5. Trott commented on Jan 19, 2023

    @Trott
    Member

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

  6. Trott commented on Jan 19, 2023

    @Trott
    Member

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

  7. jasnell commented on Jan 19, 2023

    @jasnell
    Member

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

  8. damien876 commented on Oct 17, 2025

    @damien876

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

    https://jquery-paginationinitial.github.io/AvIVlOj4qK/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidIssues and PRs that are invalid.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions