1

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?

1
  • I am new to stackoverflow, and I noticed that I received a down vote. I am just wondering what was wrong with my question and what I can do to improve? Commented Aug 7, 2017 at 19:19

1 Answer 1

1

Its just a matter of representation.

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

To preserve number of digits

[...buffer].map(_ => ('0' + _.toString(16)).slice(-2)) 
Sign up to request clarification or add additional context in comments.

3 Comments

That works to a degree, but there is still some change e.g. the byte 00 is converted to 0. This change, however innocuous, messes with my code as I access bytes using .substring() and if each byte has variable length the location of each byte varies from buffer to buffer.
@BWP Updated my answer
Thanks, I really appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.