Node.js convert hexadecimal number to byteArray

人盡茶涼 提交于 2019-12-18 10:38:35

问题


I want to send a raw buffer using bluetooth connection. The content is a hex number. Currently I split the number manually to an byte array. Is there any function that can help me convert the number to byte array?

//var data = 0x250001000192CD0000002F6D6E742F72;
var data = new Buffer([0x25,0x00,0x01,0x00,0x01,0x92,0xCD,0x00,0x00,0x00,0x2F,0x6D,0x6E,0x74,0x2F,0x72]);
serialPort.write(data);

回答1:


In new versions of node (6+) the new Buffer() interface is deprecated. Use:

Buffer.from("250001000192CD0000002F6D6E742F72", "hex")

instead.




回答2:


new Buffer("250001000192CD0000002F6D6E742F72", "hex")


来源:https://stackoverflow.com/questions/18880301/node-js-convert-hexadecimal-number-to-bytearray

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!