Python之bin2rev

使用Python将bin文件进行大小端转换:

import os
import sys
import time
import binascii
from struct import *

#FILE='test.bin'
FILE=sys.argv[1]
PACK_SIZE=4

(filename,ext) = os.path.splitext(FILE)
file_to_write = filename+'.rev'+'.bin'

def bin2rev():
    with open(FILE, 'rb') as fp_read:
        cnt = 0
        fp_write = open(file_to_write, 'wb')
        while 1:
            cont = fp_read.read(PACK_SIZE)
            if len(cont) == PACK_SIZE:
                cnt = cnt + PACK_SIZE
                for i in range(0, len(cont)):
                    tmp = pack('B' ,cont[PACK_SIZE - i - 1])
                    fp_write.write(tmp)
            else:
                fsize = os.path.getsize(FILE)
                if cnt == fsize:
                    print("Success!")
                else:
                    print("Failed!")
                break
        fp_write.close()

if len(sys.argv) > 2:
    PACK_SIZE = ord(sys.argv[2]) - 48
    if PACK_SIZE % 4 != 0:
        print("Pack size error!")
        time.sleep(1)
bin2rev()
time.sleep(1)
#os.system("pause")

将xxx.bin文件拖拽到bin2rev.py上或使用bat批处理执行带参数的python脚本即可得到大小端转换后的xxx.rev.bin文件

猜你喜欢

转载自blog.csdn.net/u011958166/article/details/79209087
rev