现在的位置: 首页 > 综合 > 正文

一个hex文件与bin文件相互转换的python代码

2014年02月15日 ⁄ 综合 ⁄ 共 2111字 ⁄ 字号 评论关闭
这还是因为想移植uClinux到proteus下那个uClinux的例子而写的,可惜折腾来折腾去还是没折腾出来,看来是自己对ARM还是不够了解,继续努力了。网上流传的一个hex和bin文件转换的程序有64K代码的限制,因而自己写了个,就当是练练手了,python是个好东西哈

import os
import sys
from struct import *
#hex to bin
def hex_bin(hexfile,binfile):    
    fin = open(hexfile)
    fout = open(binfile,'wb')
    result =''
    for hexstr in fin.readlines():
        hexstr = hexstr.strip()
        size = int(hexstr[1:3],16)
        if int(hexstr[7:9],16) != 0:
            continue    
        #end if    
        for h in range( 0, size):
            b = int(hexstr[9+h*2:9+h*2+2],16)
            result += pack('B',b)
        #end if
        fout.write(result)        
        result=''
    #end for
    fin.close()
    fout.close()

# bin to hex
def bin_hex(binfile,hexfile):
    fbin = open(binfile,'rb')
    fhex = open(hexfile,'w')
    offset = 0
    seg_addr = 0
    while 1:
        checksum=0
        result = ':'
        bindata = fbin.read(0x10)
        if len(bindata) == 0 :
            break
        #end if
        result += '%02X' % len(bindata)
        result += '%04X' % offset
        result += '00'
        checksum = len(bindata)
        checksum += (offset & 0xff) + (offset >> 8)

        for i in range(0,len(bindata)):
            byte = unpack('B',bindata[i])
            result+='%02X' % byte
            checksum += byte[0]
        #end for    
        checksum = 0x01 + ~checksum
        checksum = checksum & 0xff
        result += '%02X/n' % checksum
        fhex.write(result)
        offset += len(bindata)
        if offset == 0x10000:
            offset = 0            
            seg_addr += 1
            result = ':02000004'
            result += '%02X%02X' % ((seg_addr>>8) & 0xff,seg_addr & 0xff)
            checksum = 0x02 + 0x04 + (seg_addr>>8) + seg_addr & 0xff            
            checksum = -checksum
            result+='%02X' % (checksum & 0xff)
            result += '/n'
            fhex.write(result)
        #end if    
        if len(bindata) < 0x10:
            break
        #end if    
    #end while    
    fhex.write(':00000001FF')
    fbin.close()
    fhex.close()
#end for

    
if len(sys.argv) != 4 or (sys.argv[1] != '-h' and sys.argv[1] != '-b'):
    print 'usage:'
    print 'convert binary format to hexadecimal format: '
    print ' hexbin.py -h binfile hexfile'
    print 'convert hexadecimal format to binary format:'
    print ' hexbin.py -b hexfile binfile'
    exit(0)

    
if sys.argv[1] == '-h':
    bin_hex(sys.argv[2],sys.argv[3])
else:
    hex_bin(sys.argv[2],sys.argv[3])

【上篇】
【下篇】

抱歉!评论已关闭.