TheAlgorithms/Python · error · ValueError
Base16 encoded data is invalid: Data is not uppercase hex or
Error message
Base16 encoded data is invalid: Data is not uppercase hex or it contains invalid characters.
What it means
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.
Source
Thrown at ciphers/base16.py:54
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
if (len(data) % 2) != 0:
raise ValueError(
"""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 set(data) <= set("0123456789ABCDEF"):
raise ValueError(
"""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.
return bytes(int(data[i] + data[i + 1], 16) for i in range(0, len(data), 2))
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
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.
Example fix
# before
base16_decode('deadbeef') # ValueError: not uppercase hex
# after
base16_decode('DEADBEEF') # b'\xde\xad\xbe\xef' Defensive patterns
Strategy: validation
Validate before calling
data = data.strip().removeprefix('0x').upper()
if not set(data) <= set('0123456789ABCDEF'):
raise ValueError(f'invalid hex characters in {data!r}') Type guard
def is_uppercase_hex(s: str) -> bool:
return len(s) % 2 == 0 and set(s) <= set('0123456789ABCDEF') Try / catch
try:
base16_decode(data)
except ValueError as e:
if 'uppercase hex' in str(e):
base16_decode(data.upper())
else:
raise Prevention
- 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.
When it happens
Trigger: Calling base16_decode('this is not base64 encoded data.') as in the doctest, base16_decode('deadbeef') (lowercase), or base16_decode('0xFF') (0x prefix).
Common situations: Feeding bytes.hex() output (which is lowercase) directly to the decoder, strings with a '0x' prefix from hex literals, or whitespace from copy-paste.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Base16 encoded data is invalid: Data does not have an even n
- plain must contain only lowercase letters (a-z)
- plaintext is empty
- key is empty
- ciphertext is empty
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/e87a65e2b5119694.
Report an issue: GitHub.