hex_bin_trans.py-互联网文档类资源-CSDN下载python二进制文件和十六进制文件转换hexbin文件转换脚本须用python2运行更多下载资源、学习资料请访问CSDN下载频道.
目录
1.帮助:
2.bin2hex:
3.hex2bin
4.代码主体部分:
对hex文件和bin文件进行互相转换的python脚本:
在linux的终端terminal输入指令和参数选项,完成转换。
1.帮助:
在python脚本的路径下,输入
python2 hex_bin_trans.py -help
显示使用说明
2.bin2hex:
-bh 代表 bin文件 -> hex文件 后接bin路径和文件名 + hex路径和文件名即可
python2 hex_bin_trans.py -bh xx/org.bin xx/dst.hex
hex文件每行的BYTE数可以自由配置:如一行一个byte
20 line_byte_num = 1
3.hex2bin
参数和hex2bin类似;将-bh参数改为-hb即可:
python2 hex_bin_trans.py -hb xx/org.hex xx/dst.bin
4.代码主体部分:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#python2 hex_bin_trans.py -hb xx/org.hex xx/dst.bin
import os
import sys
from struct import *
#==================================================
#usr config
#==================================================
#define hex file each line 's byte num
line_byte_num = 1
#==================================================
#hex2bin
#==================================================
def hex_bin(hexfile,binfile):
fin = open(hexfile)
fout = open(binfile,'wb')
result = ''
for hexstr in fin.readlines():
hexstr = hexstr.strip()
hex_len = hexstr.__len__()
if hex_len % 2 == 1:
hex_len = hex_len + 1
hexstr = '0' + hexstr
for h in range(int(hex_len/2)-1,-1,-1):
b = int(hexstr[h*2:h*2+2],16)
result += pack('B',b)
fout.write(result)
result = ''
fin.close()
fout.close()
#==================================================
#bin2hex
#==================================================
def bin_hex(binfile,hexfile,line_byte_num):
fbin = open(binfile,'rb')
fhex = open(hexfile,'w')
result = ''
while 1:
bindata = fbin.read()
if len(bindata) == 0 :
break
for i in range(0,len(bindata)/line_byte_num):
for j in range(line_byte_num-1,-1,-1):
byte = unpack('B',bindata[i*line_byte_num+j])
result += '%02x' % byte
result += '\n'
fhex.write(result)
fbin.close()
fhex.close()
#==================================================
#dec2hex
#==================================================
def dec_hex(decfile,hexfile):
fin = open(decfile)
fout = open(hexfile,'w')
for decstr in fin.readlines():
decstr = decstr.strip()
b = int(decstr)
fout.write('{0:0>8x}\n'.format(b))
fin.close()
fout.close()
#==================================================
#dec2bin
#==================================================
def dec_bin(decfile,binfile):
hexfile = decfile.replace('.dec','.hex')
dec_hex(decfile,hexfile)
hex_bin(hexfile,binfile)
os.remove(hexfile)
#==================================================
#help
#==================================================
if (sys.argv[1] == '-help'):
print ('usage:')
print ('convert binary format to hexadecimal format: ')
print ('python2 hex_bin_trans.py -bh binfile hexfile')
print ('convert hexadecimal format to binary format: ')
print ('python2 hex_bin_trans.py -hb hexfile binfile')
exit(0)
#==================================================
#argv sel
#==================================================
if len(sys.argv) == 4:
if sys.argv[1] == '-bh':
bin_hex(sys.argv[2],sys.argv[3],line_byte_num)
elif sys.argv[1] == '-hb':
hex_bin(sys.argv[2],sys.argv[3])
elif sys.argv[1] == '-dh':
dec_hex(sys.argv[2],sys.argv[3])
elif sys.argv[1] == '-db':
dec_bin(sys.argv[2],sys.argv[3])
if len(sys.argv) == 3:
root = sys.argv[2]
if sys.argv[1] == '-bh':
bin_hex(sys.argv[2],root.replace('.bin','.hex'),line_byte_num)
elif sys.argv[1] == '-hb':
hex_bin(sys.argv[2],root.replace('.hex','.bin'))
elif sys.argv[1] == '-dh':
dec_hex(sys.argv[2],root.replace('.dec','.hex'))
elif sys.argv[1] == '-db':
dec_bin(sys.argv[2],root.replace('.dec','.bin'))