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