TheAlgorithms/Python · error · ValueError
Not a Valid Octal Number
Error message
Not a Valid Octal Number
What it means
Raised by octal_to_hex() in conversions/octal_to_hexadecimal.py when any character of the input (after optional '0o' prefix removal) is not in '01234567'. This catches digits 8/9 as well as letters like 'A' — note hex letters are invalid here because the input is expected to be octal, not hex.
Source
Thrown at conversions/octal_to_hexadecimal.py:31
TypeError: Expected a string as input
>>> octal_to_hex("Av")
Traceback (most recent call last):
...
ValueError: Not a Valid Octal Number
>>> octal_to_hex("")
Traceback (most recent call last):
...
ValueError: Empty string was passed to the function
"""
if not isinstance(octal, str):
raise TypeError("Expected a string as input")
if octal.startswith("0o"):
octal = octal[2:]
if octal == "":
raise ValueError("Empty string was passed to the function")
if any(char not in "01234567" for char in octal):
raise ValueError("Not a Valid Octal Number")
decimal = 0
for char in octal:
decimal <<= 3
decimal |= int(char)
hex_char = "0123456789ABCDEF"
revhex = ""
while decimal:
revhex += hex_char[decimal & 15]
decimal >>= 4
return "0x" + revhex[::-1]
if __name__ == "__main__":
import doctestView on GitHub (pinned to f5988cc097)
Solutions
- Validate the charset before calling: all(c in '01234567' for c in s.removeprefix('0o')).
- If the input is actually hex, use the hex-to-octal path or hexadecimal_conversion instead.
- Normalize/clean input (strip whitespace, lowercase, drop '0o') before validation.
Example fix
# before
octal_to_hex('Av') # ValueError: Not a Valid Octal Number
# after
s = 'Av'.removeprefix('0o')
if not s or any(c not in '01234567' for c in s):
raise ValueError(f'{s!r} is not octal')
octal_to_hex(s) Defensive patterns
Strategy: validation
Validate before calling
OCTAL_DIGITS = set('01234567')
s = octal.removeprefix('0o')
if not s or any(c not in OCTAL_DIGITS for c in s):
raise ValueError(f'{octal!r} is not a valid octal number')
octal_to_hex(s) Type guard
def is_valid_octal(s: str) -> bool:
s = s.removeprefix('0o')
return bool(s) and all(c in '01234567' for c in s) Try / catch
try:
octal_to_hex(octal)
except ValueError as e:
if 'Not a Valid Octal Number' in str(e):
log_invalid_input(octal)
else:
raise Prevention
- Validate charset 0-7 before calling
- Hex strings belong in the hex converter, not this one
- Strip whitespace and lowercase input during normalization
When it happens
Trigger: Calling octal_to_hex('Av'), octal_to_hex('8'), octal_to_hex('12 3') (space), or passing a hex string like '1FF' by mistake.
Common situations: Swapping argument order in a bidirectional converter and feeding a hex string where octal is expected; user typos; data pipelines that mix base-8 and base-16 identifiers.
Related errors
- Non-octal value was passed to the function
- Expected a string as input
- Empty string was passed to the function
- Invalid 'from_type' value: {from_type!r} Supported values a
- Invalid 'to_type' value: {to_type!r}. Supported values are:
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/b16053a20dfb2bac.
Report an issue: GitHub.