{"record":{"id":"b16053a20dfb2bac","repo":"TheAlgorithms/Python","slug":"not-a-valid-octal-number","errorCode":null,"errorMessage":"Not a Valid Octal Number","messagePattern":"Not a Valid Octal Number","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/octal_to_hexadecimal.py","lineNumber":31,"sourceCode":"    TypeError: Expected a string as input\n    >>> octal_to_hex(\"Av\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Not a Valid Octal Number\n    >>> octal_to_hex(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Empty string was passed to the function\n    \"\"\"\n\n    if not isinstance(octal, str):\n        raise TypeError(\"Expected a string as input\")\n    if octal.startswith(\"0o\"):\n        octal = octal[2:]\n    if octal == \"\":\n        raise ValueError(\"Empty string was passed to the function\")\n    if any(char not in \"01234567\" for char in octal):\n        raise ValueError(\"Not a Valid Octal Number\")\n\n    decimal = 0\n    for char in octal:\n        decimal <<= 3\n        decimal |= int(char)\n\n    hex_char = \"0123456789ABCDEF\"\n\n    revhex = \"\"\n    while decimal:\n        revhex += hex_char[decimal & 15]\n        decimal >>= 4\n\n    return \"0x\" + revhex[::-1]\n\n\nif __name__ == \"__main__\":\n    import doctest","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/octal_to_hexadecimal.py#L13-L49","documentation":"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.","triggerScenarios":"Calling octal_to_hex('Av'), octal_to_hex('8'), octal_to_hex('12 3') (space), or passing a hex string like '1FF' by mistake.","commonSituations":"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.","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."],"exampleFix":"# before\noctal_to_hex('Av')  # ValueError: Not a Valid Octal Number\n\n# after\ns = 'Av'.removeprefix('0o')\nif not s or any(c not in '01234567' for c in s):\n    raise ValueError(f'{s!r} is not octal')\noctal_to_hex(s)","handlingStrategy":"validation","validationCode":"OCTAL_DIGITS = set('01234567')\ns = octal.removeprefix('0o')\nif not s or any(c not in OCTAL_DIGITS for c in s):\n    raise ValueError(f'{octal!r} is not a valid octal number')\noctal_to_hex(s)","typeGuard":"def is_valid_octal(s: str) -> bool:\n    s = s.removeprefix('0o')\n    return bool(s) and all(c in '01234567' for c in s)","tryCatchPattern":"try:\n    octal_to_hex(octal)\nexcept ValueError as e:\n    if 'Not a Valid Octal Number' in str(e):\n        log_invalid_input(octal)\n    else:\n        raise","preventionTips":["Validate charset 0-7 before calling","Hex strings belong in the hex converter, not this one","Strip whitespace and lowercase input during normalization"],"tags":["python","validation","numeral-system","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}