最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Decoding a buffer in node.js - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Oct 25, 2014 at 6:05 thefourtheye 240k53 gold badges465 silver badges500 bronze badges asked Oct 25, 2014 at 5:39 CRockCRock 731 gold badge1 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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
发布评论

评论列表(0)

  1. 暂无评论