CRock
CRock

Reputation: 73

Decoding a buffer in node.js

I've got a buffer that

console.log(uid);
// <Buffer 04 23 81 5a 97 37 81>
console.log(uid[0]);
// 4
console.log(uid[1]);
// 35
console.log(uid.toJSON());
// [ 4, 35, 129, 90, 151, 55, 129 ]
console.log(uid.toString());
// #�Z�7�

I need the actual ocelet array (04,23,81,5a,97,37,81) as I need to output

0423815a973781

Any help on figuring out how to parse the buffer in such a manner would be greatly appreciated.

Upvotes: 6

Views: 13809

Answers (1)

thefourtheye
thefourtheye

Reputation: 239473

The default encoding parameter to Buffer.toString is utf-8. That is why you are getting the output like you mentioned in the question.

You just have to decode it with hex as the second parameter, like this

console.log(Buffer([4, 0x23, 0x81, 0x5a, 0x97, 0x37, 0x81]).toString("hex"));
// 0423815a973781

Upvotes: 5

Related Questions