{"record":{"id":"e87a65e2b5119694","repo":"TheAlgorithms/Python","slug":"base16-encoded-data-is-invalid-data-is-not-upperc","errorCode":null,"errorMessage":"Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.","messagePattern":"Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ciphers/base16.py","lineNumber":54,"sourceCode":"    ValueError: Base16 encoded data is invalid:\n    Data is not uppercase hex or it contains invalid characters.\n    >>> base16_decode('This is not base64 encoded data.')\n    Traceback (most recent call last):\n      ...\n    ValueError: Base16 encoded data is invalid:\n    Data is not uppercase hex or it contains invalid characters.\n    \"\"\"\n    # Check data validity, following RFC3548\n    # https://www.ietf.org/rfc/rfc3548.txt\n    if (len(data) % 2) != 0:\n        raise ValueError(\n            \"\"\"Base16 encoded data is invalid:\nData does not have an even number of hex digits.\"\"\"\n        )\n    # Check the character set - the standard base16 alphabet\n    # is uppercase according to RFC3548 section 6\n    if not set(data) <= set(\"0123456789ABCDEF\"):\n        raise ValueError(\n            \"\"\"Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.\"\"\"\n        )\n    # For every two hexadecimal digits (= a byte), turn it into an integer.\n    # Then, string the result together into bytes, and return it.\n    return bytes(int(data[i] + data[i + 1], 16) for i in range(0, len(data), 2))\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":36,"sourceCodeEnd":67,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/base16.py#L36-L67","documentation":"Raised by base16_decode() when the input contains characters outside the canonical uppercase hex alphabet '0123456789ABCDEF' (RFC 3548 section 6). The check `set(data) <= set('0123456789ABCDEF')` runs after the even-length check, so lowercase hex ('a'-'f'), whitespace, 'x' prefixes, and punctuation all fail.","triggerScenarios":"Calling base16_decode('this is not base64 encoded data.') as in the doctest, base16_decode('deadbeef') (lowercase), or base16_decode('0xFF') (0x prefix).","commonSituations":"Feeding bytes.hex() output (which is lowercase) directly to the decoder, strings with a '0x' prefix from hex literals, or whitespace from copy-paste.","solutions":["Uppercase the input: base16_decode(data.upper()) once length is even.","Strip prefixes/space: data.removeprefix('0x').replace(' ', '').upper().","Prefer bytes.fromhex(data) if you actually want case-insensitive lowercase-tolerant decoding."],"exampleFix":"# before\nbase16_decode('deadbeef')  # ValueError: not uppercase hex\n\n# after\nbase16_decode('DEADBEEF')  # b'\\xde\\xad\\xbe\\xef'","handlingStrategy":"validation","validationCode":"data = data.strip().removeprefix('0x').upper()\nif not set(data) <= set('0123456789ABCDEF'):\n    raise ValueError(f'invalid hex characters in {data!r}')","typeGuard":"def is_uppercase_hex(s: str) -> bool:\n    return len(s) % 2 == 0 and set(s) <= set('0123456789ABCDEF')","tryCatchPattern":"try:\n    base16_decode(data)\nexcept ValueError as e:\n    if 'uppercase hex' in str(e):\n        base16_decode(data.upper())\n    else:\n        raise","preventionTips":["Call .upper() on any hex produced by bytes.hex() before decoding.","Strip '0x' prefixes and whitespace at the input boundary.","For lowercase-tolerant needs, prefer bytes.fromhex() instead of this strict RFC-3548 decoder."],"tags":["cipher","encoding","base16","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}