#!/usr/bin/python3 def bin_2_c_array_file(bin_file, c_file): # 打开二进制文件和输出的 C 文件 with open(bin_file, 'rb') as bf, open(c_file, 'w') as cf: # 写入文件头 cf.write('#include "stdint.h"\n') #cf.write('const uint8_t int_svr_code[] __attribute__((aligned(32), section(".trap_entry"))) = {\n') cf.write('const uint8_t int_svr_code[] __attribute__((aligned(32))) = {\n') all_data = bf.read() # 将二进制内容按每4字节分割并转换为十六进制字符串 for i in range(len(all_data)): cf.write('0x%02x, ' % all_data[i]) if ((i + 1) % 16) == 0: cf.write('\n') if ((len(all_data) + 1) % 16) != 0: cf.write('\n};\n') else: cf.write('};\n') ''' wstr = '' for i in range(len(all_data)): wstr = wstr + '0x%02x, ' % all_data[i] if ((i + 1) % 16) == 0: cf.write(wstr + '\n') wstr = '' if len(wstr) != 0: cf.write(wstr) cf.write('\n};\n') ''' cf.close() bf.close() if __name__ == '__main__': import sys #import os #print(len(sys.argv), sys.argv, os.getcwd()) # 运行脚本 #bin_to_coe_file("./tb/sw/build/slvTest.bin", "./tb/sw/build/slvTest.coe") if len(sys.argv) >= 3: bin_2_c_array_file(sys.argv[1], sys.argv[2])