You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Running v17.3.0, Buffer.from("abcde") returns <Buffer 61 62 63 64 65> as expected Buffer.from("abcde").map(x=>parseInt(x,16)) returns <Buffer 97 98 99 00 01>
This is probably not the intended behavior of Buffer
Yes. Although it oddly wraps at 100 instead of 256. I think Buffer is treating the result of parseInt as 0x100 instead of 100.
I don't know the exact interface between Buffer and parseInt; perhaps this is difficult to fix and best remain a "feature" rather than a bug. It is unintuitive but perhaps it's just how Buffers work and there's no way around it.
A hexadecimal 100. Perhaps that's where the confusion comes from: console.log(buffer) prints a hexadecimal representation of the bytes. Here's an easy way to demonstrate that:
Activity
vweevers commentedon Aug 2, 2022
That's a
parseInt()behavior, unrelated to Buffer:parseInt(0x61, 16) === 0x97.nanaknihal commentedon Aug 2, 2022
That's approximately what parseInt does, which is fine. The problem seems to be from Buffer, not parseInt:
Buffer.from("abcde").map(x=>parseInt(x,16))returns<Buffer 97 98 99 00 01>instead of<Buffer 97 98 99 100 101>orsome other array with
97, 98, 99, 100, 101.vweevers commentedon Aug 2, 2022
The maximum value of a byte is 255. Buffer is a Uint8Array (rather than a Uint8ClampedArray), so values get wrapped.
nanaknihal commentedon Aug 2, 2022
Yes. Although it oddly wraps at 100 instead of 256. I think Buffer is treating the result of parseInt as 0x100 instead of 100.
I don't know the exact interface between Buffer and parseInt; perhaps this is difficult to fix and best remain a "feature" rather than a bug. It is unintuitive but perhaps it's just how Buffers work and there's no way around it.
vweevers commentedon Aug 2, 2022
A hexadecimal
100. Perhaps that's where the confusion comes from:console.log(buffer)prints a hexadecimal representation of the bytes. Here's an easy way to demonstrate that:There's no bug here.