本文介绍了Node.js将十六进制数转换为byteArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用蓝牙连接发送原始缓冲区。内容是一个十六进制数字。目前,我将数字手动拆分为一个字节数组。有什么功能可以帮助我将数字转换为字节数组?

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);


推荐答案

在新版本的节点(6+)中, new Buffer()接口已被弃用。使用:

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

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

这篇关于Node.js将十六进制数转换为byteArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 10:36