from backend.util.crypto_hash import crypto_hash

HEX_TO_BINARY_CONVERSION_TABLE = {
    '0': '0000',
    '1': '0001',
    '2': '0010',
    '3': '0011',
    '4': '0100',
    '5': '0101',
    '6': '0110',
    '7': '0111',
    '8': '1000',
    '9': '1001',
    'a': '1010',
    'b': '1011',
    'c': '1100',
    'd': '1101',
    'e': '1110',
    'f': '1111',
}


def hex_to_binary(hex_string):
    binary_string = ''

    for character in hex_string:
        binary_string += HEX_TO_BINARY_CONVERSION_TABLE[character]

    return binary_string


def main():
    number = 19345
    print(f'original_number: {number}')

    hex_number = hex(number)[2:]
    # this splice removes the 0x prefix, a common programming representation for a string in Hex format
    print(f'hex_number: {hex_number}')

    # now converting the hex to binary using the function
    binary_number = hex_to_binary(hex_number)
    print(f'binary_number: {binary_number}')

    # checking the binary number converts back to the original number - closing the loop,
    # the int 2 specifies that the variable is in binary form
    original_number = int(binary_number, 2)
    print(f'original_number_match: {original_number == number}')

    hex_to_binary_crypto_hash = hex_to_binary(crypto_hash('test_data'))
    print(f'hex_to_binary_crypto_hash: {hex_to_binary_crypto_hash}')


if __name__ == '__main__':
    main()
