encodes the given bytes into base16 base16encodeb hello world 48656c6c6f20576f726c6421 base16encodeb hello world 48454c4c4f20574f524c4421 base16encodeb turn the data into a list of integers where each integer is a byte then turn each byte into its hexadecimal representation make sure it is uppercase and then join everything together and return it decodes the given base16 encoded data into bytes base16decode 48656c6c6f20576f726c6421 b hello world base16decode 48454c4c4f20574f524c4421 b hello world base16decode b base16decode 486 traceback most recent call last valueerror base16 encoded data is invalid data does not have an even number of hex digits base16decode 48656c6c6f20576f726c6421 traceback most recent call last valueerror base16 encoded data is invalid data is not uppercase hex or it contains invalid characters base16decode this is not base64 encoded data traceback most recent call last valueerror base16 encoded data is invalid data is not uppercase hex or it contains invalid characters check data validity following rfc3548 https www ietf orgrfcrfc3548 txt base16 encoded data is invalid data does not have an even number of hex digits check the character set the standard base16 alphabet is uppercase according to rfc3548 section 6 if not setdata set0123456789abcdef raise valueerror for every two hexadecimal digits a byte turn it into an integer then string the result together into bytes and return it encodes the given bytes into base16 base16_encode b hello world 48656c6c6f20576f726c6421 base16_encode b hello world 48454c4c4f20574f524c4421 base16_encode b turn the data into a list of integers where each integer is a byte then turn each byte into its hexadecimal representation make sure it is uppercase and then join everything together and return it decodes the given base16 encoded data into bytes base16_decode 48656c6c6f20576f726c6421 b hello world base16_decode 48454c4c4f20574f524c4421 b hello world base16_decode b base16_decode 486 traceback most recent call last valueerror base16 encoded data is invalid data does not have an even number of hex digits base16_decode 48656c6c6f20576f726c6421 traceback most recent call last valueerror base16 encoded data is invalid data is not uppercase hex or it contains invalid characters base16_decode this is not base64 encoded data traceback most recent call last valueerror base16 encoded data is invalid data is not uppercase hex or it contains invalid characters check data validity following rfc3548 https www ietf org rfc rfc3548 txt base16 encoded data is invalid data does not have an even number of hex digits check the character set the standard base16 alphabet is uppercase according to rfc3548 section 6 base16 encoded data is invalid data is not uppercase hex or it contains invalid characters for every two hexadecimal digits a byte turn it into an integer then string the result together into bytes and return it
def base16_encode(data: bytes) -> str:
    
    
    
    
    return "".join([hex(byte)[2:].zfill(2).upper() for byte in list(data)])


def base16_decode(data: str) -> bytes:
    
    
    
    if (len(data) % 2) != 0:
        raise ValueError(
            
        )
    
    
    if not set(data) <= set("0123456789ABCDEF"):
        raise ValueError(
            
        )
    
    
    return bytes(int(data[i] + data[i + 1], 16) for i in range(0, len(data), 2))


if __name__ == "__main__":
    import doctest

    doctest.testmod()