{"record":{"id":"adcaa3d324622772","repo":"TheAlgorithms/Python","slug":"invalid-value-was-passed-to-the-function","errorCode":null,"errorMessage":"Invalid value was passed to the function","messagePattern":"Invalid value was passed to the function","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/hex_to_bin.py","lineNumber":43,"sourceCode":"    ValueError: Invalid value was passed to the function\n    >>> hex_to_bin(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: No value was passed to the function\n    \"\"\"\n\n    hex_num = hex_num.strip()\n    if not hex_num:\n        raise ValueError(\"No value was passed to the function\")\n\n    is_negative = hex_num[0] == \"-\"\n    if is_negative:\n        hex_num = hex_num[1:]\n\n    try:\n        int_num = int(hex_num, 16)\n    except ValueError:\n        raise ValueError(\"Invalid value was passed to the function\")\n\n    bin_str = \"\"\n    while int_num > 0:\n        bin_str = str(int_num % 2) + bin_str\n        int_num >>= 1\n\n    return int((\"-\" + bin_str) if is_negative else bin_str)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":25,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/hex_to_bin.py#L25-L57","documentation":"Raised by hex_to_bin(hex_num) in conversions/hex_to_bin.py:43 when int(hex_num, 16) fails after sign stripping — i.e. the remaining string is not a valid hexadecimal literal. Characters outside 0-9/a-f/A-F (including a second '-', dots, spaces, '0x' is accepted but 'g' is not) trigger this ValueError with the message 'Invalid value was passed to the function'.","triggerScenarios":"hex_to_bin('F-f'), hex_to_bin('hello'), hex_to_bin('12.3'), or strings containing separators/whitespace in the middle such as 'FF FF' or 'de ad beef'.","commonSituations":"Passing spaced hex dumps or colon-separated MAC-style strings directly; locale/encoding artifacts embedded in the string; missing cleanup of a leading '0x' variant or sign in the wrong position.","solutions":["Sanitize the string first: s = s.strip().lstrip('-').replace(' ', '') (and apply your sign handling) so only [0-9a-fA-F] remains","Validate with a regex before calling: re.fullmatch(r'-?[0-9a-fA-F]+', s)","Give the user a clear message by catching ValueError and re-raising with the offending input included"],"exampleFix":"# before\nhex_to_bin('de ad beef')  # ValueError\n\n# after\nclean = 'de ad beef'.replace(' ', '')\nprint(hex_to_bin(clean))","handlingStrategy":"validation","validationCode":"import re\nif not re.fullmatch(r'-?[0-9a-fA-F]+', hex_num.strip()):\n    raise ValueError(f'{hex_num!r} is not a hexadecimal string')","typeGuard":null,"tryCatchPattern":"try:\n    hex_to_bin(hex_num)\nexcept ValueError:\n    cleaned = re.sub(r'[^0-9a-fA-F]', '', hex_num)\n    print(hex_to_bin(cleaned))","preventionTips":["Strip separators (spaces, colons) from hex dumps before converting","Validate with a hex regex when accepting external input","Note '-' is only honored as a leading sign, not mid-string"],"tags":["python","value-error","hex","binary","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}