Python HDB3 AMI 编码与解码

 1 # -*- coding: cp936 -*-
 2 pre_n = '010010000100001100000000000000000000000001010001001001001010000101'
 3 print '编码过程……………………'
 4 length = len(pre_n)
 5 pre_list = list(pre_n)
 6 for ch in range (0,length):
 7     pre_list[ch] = int (pre_list[ch])
 8 store_list = pre_list
 9 print pre_list
10 ich = -1 ;
11 for ch in range (0,length):
12     if pre_list[ch] == 1 :
13         ich = ich * (-1)
14     pre_list[ch] = pre_list[ch]*ich;
15 print pre_list
16 pre_flag_num = 0;
17 flag_zero = 0;
18 for ch in range (0,length):
19     if pre_list[ch] <> 0 :
20         flag_num = pre_list[ch];
21         flag_zero = 0;
22     else:
23         flag_zero += 1;
24     if flag_zero == 4 :
25         if flag_num == pre_flag_num :
26             pre_list[ch-3] = flag_num * -1;
27             pre_list[ch] = flag_num * -1;
28             pre_flag_num = flag_num * -1;
29             for ch_temp in range(ch+1,length):
30                 pre_list[ch_temp] = pre_list[ch_temp] * -1
31         else:
32             pre_list[ch] = flag_num ;
33         pre_flag_num = pre_list[ch];
34         flag_zero = 0;
35 print pre_list,'\n 解码过程………………'
36 flag_zero = 0;
37 for ch in range (0,length):
38     if flag_zero == 2 :
39         if flag_num == pre_list[ch]:
40             flag_zero = 0
41             for ch_temp in range(ch-3, ch+1):
42                 pre_list[ch_temp] = 0;
43     if flag_zero == 3 :
44         if pre_list[ch] <> 0 :
45             flag_zero = 0;
46             for ch_temp in range(ch-3, ch+1):
47                 pre_list[ch_temp] = 0;
48     if pre_list[ch] <> 0 :
49         flag_num = pre_list[ch]
50         flag_zero = 0;
51     else:
52         flag_zero += 1;
53     pre_list[ch] = abs(pre_list[ch])
54 print pre_list
55 if pre_list is store_list:
56     print "Successed ..."

 

 

posted @ 2013-04-18 22:57  传说中的福利君  阅读(1508)  评论(0编辑  收藏  举报