python hex转bin_一个hex文件与bin文件相互转换的python代码 | 学步园

这篇博客分享了一个用Python编写的程序,能够将HEX文件转换为BIN文件,反之亦然。博主最初是为了移植uClinux到Proteus而进行的尝试,虽然未成功,但借此练习了Python编程。程序中定义了两个函数,`hex_bin`用于HEX转BIN,`bin_hex`用于BIN转HEX,并提供了命令行参数使用说明。
摘要由CSDN通过智能技术生成

这还是因为想移植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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值