The python error ValueError: Invalid literal for int with base 16: occurs when the built-in int(val, 16) function is called with a string argument which cannot be parsed as a hexa decimal number. The int(val, 16) function returns an integer object created from a string or a hexa decimal number. If the string or hexa decimal number can not convert as an integer, the error ValueError: Invalid literal for int with base 16: will be thrown
The int(val, 16) function converts the given string or hex decimal number to an integer. The default base for the int() buit-in function is 10. The digits for hex decimal number are in between 0 and 9, a to f. The hex decimal number can also have negative numbers. If the string is empty or contains a value other than a hexa number, or if the string contains a float, the error ValueError: Invalid literal for int with base 16: will be thrown.
The int(val, 16) function converts the string to an integer if the string is a valid representation of the hex decimal number and validates against the base value 16. The int(val, 16) build in function displays the error message that shows you the exact string you were trying to parse as an integer.
Exception
The error ValueError: Invalid literal for int with base 16: will be shown as below the stack trace. The stack trace shows the line that the int(val, 16) build in function fails to parse to convert an integer from a string or a hex decimal number.
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print int(x, 16)
ValueError: invalid literal for int() with base 16: ''
[Finished in 0.1s with exit code 1]
How to reproduce the error
If the build in int(val, 16) function is called with a string argument that contains an empty string, or contains a value other than a hex decimal value, or contains a float value, this error can be reproduced. In the example below, an attempt is made to pass an empty string to the build in int(val, 16) function. The error ValueError: Invalid literal for int with base 16: will be thrown as an empty string that can not be converted to an integer.
x=''
print int(x, 16)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print int(x, 16)
ValueError: invalid literal for int() with base 16: ''
[Finished in 0.1s with exit code 1]
Root Cause
If the build in int(val, 16) function is called with a string argument that contains an empty string, or contains a value other than a hex decimal value, or contains a float value, the int(val, 16) function parses the string value to an integer value as per the base 16. These arguments can not be parsed into an integer value since the string does not have a valid hex decimal value.
Valid arguments in int(val, 16) function
The following are valid arguments for the built in function int(val, 16). If you use one of the below, there will not be any error.
int(val, 16) functions with a string containing a hex decimal value – If a string having a hex integer value is passed as an argument in int(val, 16) function, returns the integer value.
print int('b', 16) # returns 11
Invalid arguments in int(val, 16) function
Below is some of the examples that will cause the error.
int(val, 16) function with an empty string – The empty string can not be parsed as an integer value
print int('', 16) # throws ValueError: invalid literal for int() with base 16: ''
int(val, 16) function with an integer value – If an integer value is passed as an argument in int(val, 16) function, it will throw the value error
print int( 5, 16) # throws TypeError: int() can't convert non-string with explicit base
int(val, 16) function with a float value – If a float value is passed as an argument in int(val, 16) function, it will throw the value error
print int(5.4, 16) # throws TypeError: int() can't convert non-string with explicit base
int(val, 16) function with a string having a float value – If a string having a float value is passed as an argument, int(val, 16) function will throw value error.
print int('5.4', 16) # throws ValueError: invalid literal for int() with base 16: '5.4'
int(val, 16) function with a non-hex string – If a string contains a non-hex values such as characters and passed as an argument, the int(val, 16) function will throw value error.
print int('q', 16) # throws ValueError: invalid literal for int() with base 16: 'q'
int(val, 16) function with a boolean value – If a boolean value is passed as an argument in int(val, 16) function, it throws the value error.
print int(True, 16) # throw TypeError: int() can't convert non-string with explicit base
int(val, 16) function with a hex format value – If a hex format string is passed as an argument in int(val, 16) function, it throws the value error.
print int('\xa', 16) # throw TypeError: int() can't convert non-string with explicit base