// 生成十六进制数据
const hexData = '7E02000000010000000aB77E';
// 这里是示例的十六进制数据
// 将十六进制数据转换为字节数组
const data = hexToBytes(hexData);
// 创建 Blob 对象
const blob = new Blob([data], { type: 'application/octet-stream' });
// 创建下载链接
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.hex';```
// 触发下载
```downloadLink.click();
console.log('十六进制文件生成成功!',CRC.ToModbusCRC16('7E020000000100000065'));```
// 辅助函数:将十六进制字符串转换为字节数组
function hexToBytes(hex) {
const bytes = [];
for (let i = 0; i < hex.length; i += 2) {
bytes.push(parseInt(hex.substr(i, 2), 16));
}
return new Uint8Array(bytes);
}
标签:十六进制,const,二进制,downloadLink,前端,hex,bytes,下载
From: https://www.cnblogs.com/wutong-211/p/17693505.html