python16进制转2进制流_将十六进制文件转换为二进制文件
我已經(jīng)把它重寫得更干凈一些;根據(jù)十六進(jìn)制文件的確切格式,您可能需要對(duì)其進(jìn)行一些修改,但這應(yīng)該是一個(gè)很好的起點(diǎn)。在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.write(''.join(chr(i) for i in data))
if VERBOSE: print(' {0} bytes written'.format(len(data)))
def main(input, output):
data = read_hexfile(input)
write_binfile(output, data)
if __name__=="__main__":
parser = optparse.OptionParser()
parser.add_option('-i', ' input', dest='input', help='name of HEX input file')
parser.add_option('-o', ' output', dest='output', help='name of BIN output file')
parser.add_option('-v', ' verbose', dest='verbose', help='output extra status information', action='store_true', default=False)
(options, args) = parser.parse_args()
VERBOSE = options.verbose
main(options.input, options.output)
總結(jié)
以上是生活随笔為你收集整理的python16进制转2进制流_将十六进制文件转换为二进制文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python pillow环境_Pyth
- 下一篇: python做圆柱绕流_圆柱绕流