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

Unexpected behavior of Buffer.from( num.toString(16), "hex" ) #21242

Closed
fider opened this issue Jun 10, 2018 · 3 comments
Closed

Unexpected behavior of Buffer.from( num.toString(16), "hex" ) #21242

fider opened this issue Jun 10, 2018 · 3 comments
Labels
buffer Issues and PRs related to the buffer subsystem. feature request Issues that request new features to be added to Node.js.

Comments

@fider
Copy link

fider commented Jun 10, 2018

Info:
Version: v8.10.0
Platform: Windows 10, 64-bit
Subsystem: buffer module: Buffer.from

Code:

let hex_string = ('abc').toString(16);
let b = Buffer.from( hex_string, 'hex' );

Problem:
b == <Buffer ab> and I expect that b == <Buffer 0a bc>

Details and explanation:
When calling Buffer.from( ... , 'hex') with hex string that have odd number of characters (in case when no leading zero provided) I expect Buffer.from to add leading zero in case of hex encoding, so "1" will be recognized as "01".

Use case example:

Buffer.from( someNumber.toString(16), 'hex' ); 

I expect that in above example I do not have to append on my own leading "0" in case when toString(16) returns odd number of characters.

Other solution I see (instead of fixing Buffer.from) is to update toString(16), but in my opinion fix should implemented inside Buffer.from.

@vsemozhetbyt
Copy link
Contributor

FWIW, this behavior seems documented:

  • 'hex' - Encode each byte as two hexadecimal characters.

@vsemozhetbyt vsemozhetbyt added buffer Issues and PRs related to the buffer subsystem. feature request Issues that request new features to be added to Node.js. labels Jun 10, 2018
@bnoordhuis
Copy link
Member

Yes, it's the documented behavior. 'hex' has always behaved this way and it's not something we'll change, that would be massively backwards incompatible.

@jrasanen
Copy link
Contributor

I think Uint8Array from V8 does this conversion. So before calling it, we should do the left padding with zeroes?

orenyomtov added a commit to orenyomtov/lodestar that referenced this issue Nov 8, 2023
Related to this "documented behavior" quirk of `Buffer.from`:
nodejs/node#21242
nazarhussain pushed a commit to ChainSafe/lodestar that referenced this issue Nov 29, 2023
* Fix `hexToBuffer` bug

Related to this "documented behavior" quirk of `Buffer.from`:
nodejs/node#21242

* Fix lint

* Implement in multiple lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

4 participants