Node.js: 创建 8 字节缓冲区
Node.js: Creating 8 byte buffer
我希望将 Python 片段翻译成 Node.js。 Python 代码如下所示:
NULL_CHAR = chr(0)
def write_report(report):
with open('/dev/hidg0', 'rb+') as fd:
fd.write(report.encode())
# write_report(<byte1> + <byte3> + <byte4> + <bytes5-8>)
write_report(chr(32)+NULL_CHAR+chr(11)+NULL_CHAR*5)
如您所见,Python 代码非常直观地构建了 8 字节报告。
如何在 Node.js 中执行同样的操作?我目前的猜测是使用缓冲区 class:
const data = Buffer.from([0x20, 0x0, 0xB, 0x0, 0x0, 0x0, 0x0, 0x0]);
所以一共:
const fs = require('fs');
var file = '/dev/hidg0'
const data = Buffer.from([0x20, 0x0, 0xB, 0x0, 0x0, 0x0, 0x0, 0x0]);
fs.writeFile(file, data, (err) => {
if (err) throw err;
});
这样做正确吗?提前致谢
果然,答案就在问题中 - 有效
我希望将 Python 片段翻译成 Node.js。 Python 代码如下所示:
NULL_CHAR = chr(0)
def write_report(report):
with open('/dev/hidg0', 'rb+') as fd:
fd.write(report.encode())
# write_report(<byte1> + <byte3> + <byte4> + <bytes5-8>)
write_report(chr(32)+NULL_CHAR+chr(11)+NULL_CHAR*5)
如您所见,Python 代码非常直观地构建了 8 字节报告。
如何在 Node.js 中执行同样的操作?我目前的猜测是使用缓冲区 class:
const data = Buffer.from([0x20, 0x0, 0xB, 0x0, 0x0, 0x0, 0x0, 0x0]);
所以一共:
const fs = require('fs');
var file = '/dev/hidg0'
const data = Buffer.from([0x20, 0x0, 0xB, 0x0, 0x0, 0x0, 0x0, 0x0]);
fs.writeFile(file, data, (err) => {
if (err) throw err;
});
这样做正确吗?提前致谢
果然,答案就在问题中 - 有效