我已经把它重写得更干净一些;根据十六进制文件的确切格式,您可能需要对其进行一些修改,但这应该是一个很好的起点。在import os
import sys
import optparse
VERBOSE = False
def read_hexfile(fname):
if VERBOSE: print('Reading from {0}'.format(fname))
data = False
res = []
with open(fname, 'r') as inf:
for line in inf:
if data:
if line[:8] == '03 00 01':
data = False
else:
res.extend(int(hex, 16) for hex in line.split())
else:
if line[:7] == '/origin':
data = True
else:
# skip non-data
pass
if VERBOSE: print(' {0} bytes read'.format(len(res)))
return res
def write_binfile(fname, data):
if VERBOSE: print('Writing to {0}'.format(fname))
with open(fname, 'wb') as outf:
outf.writ