BWP
BWP

Reputation: 390

Converting Node Buffer to Array Changes Base

I have a buffer that contains 126 hexadecimal bytes:

<Buffer 01 00 5e 57 15 02 00 1e 67 d0 bc d8 08 00 45 00 00 70 90 21 40 00 40 11 f8 1d 17 e2 9b 82 e9 d7 15 02 28 88 28 88 00 5c ae aa 01 00 02 80 01 00 00 00 ... >

And I change it to an array like so:

console.log([...Buffer]);

However, this outputs all the bytes in the buffer converted to base 10:

1,0,94,87,21,2,0,30,103,208,188,216,8,0,69,0,0,112,164,203,64,0,64,17,227,11...

What I want is to put all the buffer's bytes into an array without changing their base. What would be the best way to do this?

Upvotes: 1

Views: 122

Answers (1)

user2065736
user2065736

Reputation:

Its just a matter of representation.

[...buffer].map(b => b.toString(16))

To preserve number of digits

[...buffer].map(_ => ('0' + _.toString(16)).slice(-2)) 

Upvotes: 1

Related Questions