def generate_hex_file(data, output_file):
with open(output_file, 'w') as f:
address = 0
while data:
line_data = data[:32]
data = data[32:]
line_len = len(line_data)
checksum = (line_len + (address >> 8) + (address & 0xFF) + 0 + sum(line_data)) & 0xFF
checksum = (~checksum + 1) & 0xFF
line = f':{line_len:02X}{address:04X}00' + ''.join(f'{byte:02X}' for byte in line_data) + f'{checksum:02X}'
f.write(line + '\n')
address += line_len
# 示例数据
data = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88] * 100 # 重复的数据
output_file = 'output.hex'
generate_hex_file(data, output_file)
|