{"record":{"id":"8b6f95c3206964d2","repo":"TheAlgorithms/Python","slug":"non-hexadecimal-value-was-passed-to-the-function","errorCode":null,"errorMessage":"Non-hexadecimal value was passed to the function","messagePattern":"Non-hexadecimal value was passed to the function","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/hexadecimal_to_decimal.py","lineNumber":39,"sourceCode":"        ...\n    ValueError: Non-hexadecimal value was passed to the function\n    >>> hex_to_decimal(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Empty string was passed to the function\n    >>> hex_to_decimal(\"12m\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Non-hexadecimal value was passed to the function\n    \"\"\"\n    hex_string = hex_string.strip().lower()\n    if not hex_string:\n        raise ValueError(\"Empty string was passed to the function\")\n    is_negative = hex_string[0] == \"-\"\n    if is_negative:\n        hex_string = hex_string[1:]\n    if not all(char in hex_table for char in hex_string):\n        raise ValueError(\"Non-hexadecimal value was passed to the function\")\n    decimal_number = 0\n    for char in hex_string:\n        decimal_number = 16 * decimal_number + hex_table[char]\n    return -decimal_number if is_negative else decimal_number\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n","sourceCodeStart":21,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/hexadecimal_to_decimal.py#L21-L50","documentation":"Raised by hex_to_decimal(hex_string) in conversions/hexadecimal_to_decimal.py:39 when, after lowercasing, stripping, and removing a leading '-', any remaining character is not in the hex_table (0-9, a-f). This is a strict per-character check, so even one bad character ('12m', '0x1f' because 'x' is not a hex digit) fails the whole conversion with ValueError('Non-hexadecimal value was passed to the function').","triggerScenarios":"hex_to_decimal('12m'), hex_to_decimal('0x1f') ('x' rejected — the function does not accept the 0x prefix), hex_to_decimal('ff ff'), or hex strings with signs anywhere but position 0.","commonSituations":"Forgetting to strip a '0x'/'0X' prefix from formatter output; hex dumps containing spaces, colons, or newlines; mixed-case is fine (input is lowercased) but prefixes and separators are not.","solutions":["Strip the prefix before calling: s = s.lower().removeprefix('0x') and remove separators (spaces, colons, dashes)","Pre-validate: re.fullmatch(r'-?[0-9a-fA-F]+', s)","If input may be any-base, use int(s, 16) inside your own try/except ValueError and map to a user-facing message"],"exampleFix":"# before\nhex_to_decimal('0x1f')  # ValueError\n\n# after\nhex_to_decimal('0x1f'.removeprefix('0x'))  # 31","handlingStrategy":"validation","validationCode":"import re\nif not re.fullmatch(r'-?[0-9a-fA-F]+', hex_string.strip()):\n    raise ValueError(f'{hex_string!r} is not hexadecimal')","typeGuard":null,"tryCatchPattern":"try:\n    hex_to_decimal(hex_string)\nexcept ValueError as e:\n    if 'Non-hexadecimal' in str(e):\n        hex_string = hex_string.lower().removeprefix('0x').replace(' ', '')\n        result = hex_to_decimal(hex_string)\n    else:\n        raise","preventionTips":["removeprefix('0x') on formatter output before calling","Remove whitespace/colons from pasted hex dumps","Remember input is lowercased internally, so case is safe but prefixes are not"],"tags":["python","value-error","hex","decimal","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}