|
| 1 | +""" |
| 2 | +Binary: Base16 |
| 3 | +
|
| 4 | +Conversions from base16 to: |
| 5 | + - base2 |
| 6 | + - base10 |
| 7 | + - ASCII |
| 8 | +
|
| 9 | +Author: Ian Doarn |
| 10 | +""" |
| 11 | +from pygorithm.binary.base2 import to_ascii as b2_to_ascii |
| 12 | +from math import pow |
| 13 | + |
| 14 | +HEX_BINARY_VALUES = { |
| 15 | + '0': '0000', '1': '0001', '2': '0010', '3': '0011', |
| 16 | + '4': '0100', '5': '0101', '6': '0110', '7': '0111', |
| 17 | + '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', |
| 18 | + 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111' |
| 19 | +} |
| 20 | + |
| 21 | +HEX_LETTER_VALUES = { |
| 22 | + '0': 0, '1': 1, '2': 2, |
| 23 | + '3': 3, '4': 4, '5': 5, |
| 24 | + '6': 6, '7': 7, '8': 8, |
| 25 | + '9': 9, 'A': 10, 'B': 11, |
| 26 | + 'C': 12, 'D': 13, 'E': 14, |
| 27 | + 'F': 15 |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +def to_base2(h, visualize=False): |
| 32 | + """ |
| 33 | + Convert hexadecimal to binary number |
| 34 | +
|
| 35 | + :param h: hexadecimal number |
| 36 | + :param visualize: Show process |
| 37 | + :return: binary number |
| 38 | + """ |
| 39 | + |
| 40 | + hex_char_list = list(h) |
| 41 | + _list = [] |
| 42 | + |
| 43 | + for value in hex_char_list: |
| 44 | + if visualize: |
| 45 | + print("{} -> {}".format( |
| 46 | + value, HEX_BINARY_VALUES[value] |
| 47 | + )) |
| 48 | + _list.append(HEX_BINARY_VALUES[value]) |
| 49 | + |
| 50 | + return int(''.join(_list)) |
| 51 | + |
| 52 | + |
| 53 | +def to_base10(h, visualize=False): |
| 54 | + """ |
| 55 | + Convert hexadecimal number to decimal number |
| 56 | +
|
| 57 | + Send hex to a list and reverse. Evaluate each hex value |
| 58 | + via HEX_LETTER_VALUES map. Enumerate the list, |
| 59 | +
|
| 60 | + using the equation: value * 16 ^ index |
| 61 | +
|
| 62 | + value is the hex char value: F -> 15 |
| 63 | + index is its position in the list: ['1', 'A', 'F'] F's index = 2 |
| 64 | +
|
| 65 | + Continue this for each hex letter until we reach the end of the list, |
| 66 | + summing all evaluated values. |
| 67 | +
|
| 68 | + :param h: hexadecimal number |
| 69 | + :param visualize: Show process |
| 70 | + :return: decimal number |
| 71 | + """ |
| 72 | + |
| 73 | + # Check to see if '0x' is at the beginning and remove it |
| 74 | + if h[0:2] == '0x': |
| 75 | + hex_char_list = list(h[2:])[::-1] |
| 76 | + else: |
| 77 | + hex_char_list = list(h)[::-1] |
| 78 | + |
| 79 | + value = 0 |
| 80 | + |
| 81 | + for i, v in enumerate(hex_char_list): |
| 82 | + if visualize: |
| 83 | + print("{} -> {} || {} * (16 ^ {}) = {}".format( |
| 84 | + v, str(HEX_LETTER_VALUES[v]), |
| 85 | + str(HEX_LETTER_VALUES[v]), |
| 86 | + str(i), |
| 87 | + str(HEX_LETTER_VALUES[v] * (pow(16, i))) |
| 88 | + )) |
| 89 | + |
| 90 | + value += HEX_LETTER_VALUES[v] * (pow(16, i)) |
| 91 | + |
| 92 | + return int(value) |
| 93 | + |
| 94 | + |
| 95 | +def to_ascii(h_array, visualize=False): |
| 96 | + """ |
| 97 | + Convert base16 array to ASCII string |
| 98 | +
|
| 99 | + Input must be a list of strings: |
| 100 | + Example:: |
| 101 | +
|
| 102 | + array = [ |
| 103 | + '74', '68', '65', |
| 104 | + '20', '71', '75', |
| 105 | + '69', '63', '6B', |
| 106 | + '20', '62', '72', |
| 107 | + '6F', '77', '6E', |
| 108 | + '20', '66', '6F', |
| 109 | + '78', '20', '6A', |
| 110 | + '75', '6D', '70', |
| 111 | + '73', '20', '6F', |
| 112 | + '76', '65', '72', |
| 113 | + '20', '74', '68', |
| 114 | + '65', '20', '6C', |
| 115 | + '61', '7A', '79', |
| 116 | + '20', '64', '6F', |
| 117 | + '67' |
| 118 | + ] |
| 119 | +
|
| 120 | + result -> the quick brown fox jumps over the lazy dog |
| 121 | +
|
| 122 | + :param h_array: hex value array |
| 123 | + :param visualize: Show process |
| 124 | + :return: ASCII string |
| 125 | + """ |
| 126 | + string = '' |
| 127 | + |
| 128 | + for h_value in h_array: |
| 129 | + if visualize: |
| 130 | + print("{} -> {} -> {}".format( |
| 131 | + h_value, |
| 132 | + to_base2(h_value), |
| 133 | + b2_to_ascii(str(to_base2(h_value))) |
| 134 | + )) |
| 135 | + |
| 136 | + string += b2_to_ascii(str(to_base2(h_value))) |
| 137 | + |
| 138 | + return string |
0 commit comments