nodejs将hex文件转bin文件

发布时间 2023-05-17 15:57:19作者: chendaleiQ

image

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