YKT文件解析

YKT文件爲二進制文件,使用notepad++的HEX-Editor打開後爲python

爲了快速查看文件內容,用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
相關文章
相關標籤/搜索