Node.js 将十六进制数转换为 byteArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18880301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js convert hexadecimal number to byteArray
提问by GingerJim
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);
回答by Brad
In new versions of node (6+) the new Buffer()interface is deprecated. Use:
在节点 (6+) 的新版本中,new Buffer()不推荐使用该接口。用:
Buffer.from("250001000192CD0000002F6D6E742F72", "hex")
instead.
反而。
回答by SLaks
new Buffer("250001000192CD0000002F6D6E742F72", "hex")

