Hex String to Decimal converter
I need to write a function that takes in only a string representing a hex number and returns its decimal equivalent, or -1 if the string connotes to a negative number / has any invalid characters. I cant use any library functions in this function and it must be case insensitive.
Ive started to code it but im stuck. I started by checking if all of the characters were valid by checking there ASCII values but i dont really know where to go from here.
Code:
int hexToDecimal(char * str)
{
int i;
for(i = 0; str[i] != '\0'; i++)
{
if( ( (int) str[i] < 48 || (int) str[i] > 57) && ( (int) str[i] < 65 ||
(int) str[i] > 70) && ( (int) str[i] < 97 || (int) str[i] > 102 ))
return -1;
return 0;
}