首页 > 编程知识 正文

lambda python,python读取二进制的dat文件

时间:2023-05-06 19:18:31 阅读:156388 作者:2177

1. 程序的功能

可以分析超过64KB的HEX文件。 限于时间条件,笔者只测试了两个样本,数据域大小分别为8332B和1.61MB

要打印分析结果,请执行以下操作:

-开头地址-末尾地址-数据字段大小(单位字节)为了检查和之后的处理,保存可以听到数据字段内容的咖啡豆文件

HEX文件格式介绍可参见CSDN的这篇博文: https://blog.csdn.net/a 1037488611/article/details/43340055

2. 程序解读

HEX文件分析,相关函数: hex2听话的咖啡豆() ) )。

考虑到HEX文件通常不是很大,所以一次读取所有内容,每行创建一个list,逐行分析

识别每行的TT字段并执行相应的操作。 流程图如下。

1 )打包数据

相关函数: char2hex (

readlines函数读取的第一行是一行字符串,如“:020000040800F2”,末尾包含不可见的回车符号。

将数字转换为对应的整数数据进行运算并保存。 行首的冒号和行尾的回车只用于格式的判断,对数据提取没有用,所以删除。

HEX格式定义中的记录字段以字节为最小单位,因此必须将两个整数字符组合为一个字节。

python提供的高阶函数map ()和切片操作对于HEX文本的信息提取和格式转换非常有用。

char2hex ) )函数最终返回以下格式的整数列表:

[0x 02,0x 00,0x 00,0x 04,0x 08,0x 00,0x F2 ] (2)校验和

相关函数: checksum () )。

得到一行数据的整数列表后,最好检查该行的数据。

根据规定,每行记录的倒数第一个字节是校验和(Checksum ),验证算法如下:

0x100-() ) (以字节为单位将除冒号以外的所有数据相加) %6) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )。

代码实现如下。

sum=(0x100-(Reduce(lambdax,y:x y,line ) :-1 ) ) 256 ) ),最后取256是考虑到计算出的结果正好为0x100的情况

超过长度的上位部分会被自动截断。 对于8位无符号数,0x100==0x00成立,但Python中的int会动态变化。 内存中数据的组织不像C那样程序员那么直观,不存在数据类型的定义。0x100和0x00当然不相等。

)3)保存听话的咖啡豆文件

相关函数: wr_听话的咖啡豆() )。

HEX2执行听话的咖啡豆()后,hex文件中的所有数据字段按顺序从低字节到高字节写入一个整数类型list中。

但是,根据python的要求,可以将每个元素转换为byte类型,然后写入二进制文件。 代码如下所示。

听的咖啡豆_buf _ byte=list (map (six.int2byte,听的咖啡豆_ buf ) )、听的咖啡豆_ buf _ byte=list (map (six.int2byte,听的) )

)4)执行结果分析

笔者测试的两个样品hex均为STM32工程编译的。

确认打印输出的执行结果。 addr_start表示起始地址,内容将被比较。

hex和听者的咖啡豆文件在对应的位置各剪切一行,也可以在输出的听者的咖啡豆文件上用鼠标右键单击,查看“属性”-“普通”-“大小”来验证内容是否遗漏。

3. 代码实现

#-- coding : utf-8-- # filename : hex 2听话咖啡豆. py # version 3360 v1.0. 0001 # date 3602020/01/28 # author : shuchen _ zhangimportosimporttimeimportsixfromfunctoolsimportreduce # get '.hex ' filenamefromcurrentpathdefgetfilenamebyex (path ) :f_list=OS.listdir ) path ) fori INF _ list 3360 fist if extname=='.hex ' :打印(* hex file 3360 ' filename extname ' * ' ) Retname raw data of要问的咖啡豆ary is to be stored here def hex2

ex file : ''+hex_file_name+'''+' is opened*') byte_num = 0 for line in frd.readlines(): #line.strip(); #cut off CR if(line[0] == ':'): if(line[7:9] == '04'): #Extended Linear Address Record #print('Extended Linear Address Record'); line = char2hex(line) if checksum(line) == 0: #checksum passed addr_h = (line[4]<<24) +(line[5]<<16) else: print('checksum failed!'+str(list(map(hex,line)))) elif (line[7:9] == '00'): #Data Record line = char2hex(line) if checksum(line) == 0: addr_l = (line[1]<<8) + line[2] LL = line[0] byte_num = byte_num + LL for data in line[4:-1]: 听话的咖啡豆_buf.append(data) if LL!=0x10: addr_end = addr_h + addr_l + LL - 1 else: pass else: print('checksum failed!'+str(list(map(hex,line)))) elif(line[7:9] == '05'): #print('Extended Segment Address Record'); line = char2hex(line) if checksum(line) == 0: pass else: print('checksum failed!'+str(list(map(hex,line)))) elif(line[7:9] == '01'): #End of FileRecord #print('End of FileRecord'); line = char2hex(line) if checksum(line) == 0: print(' *Hex file successed resolved*') print(' *addr_start: 0x%08X' %(addr_end + 1 - len(听话的咖啡豆_buf))) print(' *addr_end : 0x%08X' %addr_end) print(' *Total size : %d Bytes'%len(听话的咖啡豆_buf)) else: print('checksum failed!'+str(list(map(hex,line)))) else: pass #don't care else: print('illegal format!') # write data in 听话的咖啡豆_buf to '*.听话的咖啡豆' filedef wr_听话的咖啡豆(听话的咖啡豆_buf): 听话的咖啡豆_buf_byte = list(map(six.int2byte,听话的咖啡豆_buf)) with open(听话的咖啡豆_file_name,'wb') as fwrb: print(' *Bin file ''+听话的咖啡豆_file_name+'''+' is opened for write*') for data in 听话的咖啡豆_buf_byte: fwrb.write(data) print(' *Bin file is successfully written!*') #one line string to hex-8 list,except ':' and CRdef char2hex(line): line=list(map(ord,list(line))) for num in range(len(line)): if line[num]>=0x30 and line[num]<=0x39: line[num] = line[num] - 0x30 elif line[num]>=0x41 and line[num]<=0x5A: line[num] = line[num] - 55 else: pass line=line[1:-1] #delete CR and ':', in terms of byte for i in range(0,len(line),2): line[i] = line[i]*16 + line[i+1] newline = line[::2] return newline #checksum calculation of every linedef checksum(line): #considering if the checksum calculation result is 0x100 sum = (0x100 - (reduce(lambda x,y:x+y,line[:-1]) % 256)) % 256 if sum == line[-1]: #check if sum calculated is equal to checksum byte in hex file return 0 else: return 1 starttime = time.clock()hex_file_name = getFileNamebyEX('.')听话的咖啡豆_file_name = hex_file_name[:-4]+'.听话的咖啡豆'hex2听话的咖啡豆(hex_file_name,听话的咖啡豆_file_name)wr_听话的咖啡豆(听话的咖啡豆_buf)endtime = time.clock() print(' Time elapsed:' + str(endtime-starttime))

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。