Rev 856 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 856 | Rev 869 | ||
|---|---|---|---|
| 1 | 1 | ||
| 2 | def swap_dictionary(original_dict): |
2 | def swap_dictionary(original_dict): |
| 3 | return dict([(v, k) for (k, v) in original_dict.iteritems()]) |
3 | return dict([(v, k) for (k, v) in original_dict.iteritems()]) |
| 4 | 4 | ||
| 5 | def int2hex(number): |
5 | def int2hex(number): |
| 6 | return str("%X" % number) |
6 | return str("%X" % number) |
| 7 | 7 | ||
| 8 | int2bin = lambda n: n>0 and int2bin(n>>1)+str(n&1) or '' |
8 | int2bin = lambda n: n>0 and int2bin(n>>1)+str(n&1) or '' |
| 9 | 9 | ||
| 10 | def bin2hex(number): |
10 | def bin2hex(number): |
| 11 | s = '' |
11 | s = '' |
| 12 | t = { '0000':'0', |
12 | t = { '0000':'0', |
| 13 | '0001':'1', |
13 | '0001':'1', |
| 14 | '0010':'2', |
14 | '0010':'2', |
| 15 | '0011':'3', |
15 | '0011':'3', |
| 16 | '0100':'4', |
16 | '0100':'4', |
| 17 | '0101':'5', |
17 | '0101':'5', |
| 18 | '0110':'6', |
18 | '0110':'6', |
| 19 | '0111':'7', |
19 | '0111':'7', |
| 20 | '1000':'8', |
20 | '1000':'8', |
| 21 | '1001':'9', |
21 | '1001':'9', |
| 22 | '1010':'a', |
22 | '1010':'a', |
| 23 | '1011':'b', |
23 | '1011':'b', |
| 24 | '1100':'c', |
24 | '1100':'c', |
| 25 | '1101':'d', |
25 | '1101':'d', |
| 26 | '1110':'e', |
26 | '1110':'e', |
| 27 | '1111':'f'} |
27 | '1111':'f'} |
| 28 | c = 0 |
28 | c = 0 |
| 29 | while c < len(number)-1: |
29 | while c < len(number)-1: |
| 30 | s += t[number[c:c+4]] |
30 | s += t[number[c:c+4]] |
| 31 | c += 4 |
31 | c += 4 |
| 32 | 32 | ||
| 33 | return s |
33 | return s |
| 34 | 34 | ||
| 35 | 35 | ||
| 36 | def hex2bin(number): |
36 | def hex2bin(number): |
| 37 | number = number.lower() |
37 | number = number.lower() |
| 38 | s = '' |
38 | s = '' |
| 39 | t = { '0':'0000', |
39 | t = { '0':'0000', |
| 40 | '1':'0001', |
40 | '1':'0001', |
| 41 | '2':'0010', |
41 | '2':'0010', |
| 42 | '3':'0011', |
42 | '3':'0011', |
| 43 | '4':'0100', |
43 | '4':'0100', |
| 44 | '5':'0101', |
44 | '5':'0101', |
| 45 | '6':'0110', |
45 | '6':'0110', |
| 46 | '7':'0111', |
46 | '7':'0111', |
| 47 | '8':'1000', |
47 | '8':'1000', |
| 48 | '9':'1001', |
48 | '9':'1001', |
| 49 | 'a':'1010', |
49 | 'a':'1010', |
| 50 | 'b':'1011', |
50 | 'b':'1011', |
| 51 | 'c':'1100', |
51 | 'c':'1100', |
| 52 | 'd':'1101', |
52 | 'd':'1101', |
| 53 | 'e':'1110', |
53 | 'e':'1110', |
| 54 | 'f':'1111'} |
54 | 'f':'1111'} |
| 55 | c = 0 |
55 | c = 0 |
| 56 | while c < len(number): |
56 | while c < len(number): |
| 57 | # print "\thex2bin: " + number[c] + " - " + t[number[c]] |
57 | # print "\thex2bin: " + number[c] + " - " + t[number[c]] |
| 58 | s += t[number[c]] |
58 | s += t[number[c]] |
| 59 | c += 1 |
59 | c += 1 |
| 60 | 60 | ||
| 61 | return s |
61 | return s |