const fs = require('fs');
fs.readFile('./SmartLockMain.hex', 'utf-8', (err, data) => {
if (err) throw err;
const lines = data.split('\n');
const outBuffer = [];
for (const line of lines) {
if (line.startsWith(':') && line.trim().length >= 22) { // 过滤行长度小于 20 的行,根据需求加条件
const hexData = line.substr(1).trim();
const byteData = Buffer.from(hexData, 'hex');
outBuffer.push(byteData.slice(4, -1));
}
}
fs.writeFile('output.bin', Buffer.concat(outBuffer), (err) => {
if (err) throw err;
});
});
标签:bin,文件,fs,const,err,hex,outBuffer,line
From: https://www.cnblogs.com/chendaleiQQ/p/17409030.html