Skip to content

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

@iamrenejr

Description

@iamrenejr
  • Version: 10.16.3
  • Platform: Darwin Renes-MacBook-Pro.local 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64
  • Subsystem: Buffer

Run these in the REPL:

 Buffer.from('0', 'hex').toString('hex')               // returns ''
 Buffer.from('08', 'hex').toString('hex')              // returns '08'
 Buffer.from('088', 'hex').toString('hex')             // returns '08'
 Buffer.from('0888', 'hex').toString('hex')            // returns '0888'
 Buffer.from('08888', 'hex').toString('hex')           // returns '0888'
 Buffer.from('088888', 'hex').toString('hex')          // returns '088888'

I expected .toString('hex') to be the inverse operation of Buffer.from(x, 'hex'), but it doesn't seem to be the case. Why not?

I was able to catch this error using fast-check and it found a few other strings that behave this way:

  • 8501eb788
  • eb788
  • 788

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.docIssues and PRs related to the documentations.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions