YKT文件解析

YKT文件为二进制文件,使用notepad++的HEX-Editor打开后为

为了快速查看文件内容,用python代码解析

 1 from struct import *
 2 
 3 '''
 4 大端小端问题:http://blog.csdn.net/fan_hai_ping/article/details/8424360
 5              http://blog.csdn.net/lis_12/article/details/52777983
 6 '''
 7 def ReadYKTFile():
 8     A3 = '<bqqhhhiibhiq'
 9     
10     f = open(u'D:\jie\02_测试数据\YKT\20170901\52013170901000000001J0','rb')
11     #<小端模式
12     print unpack('<bbbhhhiibbqqqqqqqqqq',f.read(1+1+1+2+2+2+4+4+1+1+8+8+8+8+8+8+8+8+8+8))
13     
14     f.close()
15     f = open(u'D:\jie\02_测试数据\YKT\20170901\52013170901000000001J0','rb')
16     print unpack('@bbbhhhiibbqqqqqq',f.read(72))
17     f.close()
18     
19     #print calcsize("bbbhhhiibbqqqqqqqqqq") 
20     #print calcsize("<bbbhhhiibbqqqqqqqqqq") 
21     print BCDtoDatetime(17372960,4) #20170901,日期格式
22     print BCDtoDatetime(320869749, 4)
23     
24 '''
25 BCD编码是4个bit位表示一个数字
26 
27 参数:x需要转换的数字,y字节数
28 '''
29 def BCDtoDatetime(x,y):
30     #s = str(bin(x))
31     s = ''
32     temp = ''
33     for i in range(y*2):
34         #print i,x
35         m,n = divmod(x,pow(16,y*2-(i+1)))
36         #print m,n
37         x = n
38         if((i)%2>0):
39             temp = temp + str(m)
40             #print temp
41             s = str(temp)+s
42             temp = ''            
43         else:
44             temp = str(m)
45     return s
原文地址:https://www.cnblogs.com/Thriving-Heart/p/7651203.html