# Thread: Converting hexidecimal digits to integer value

1. Originally Posted by laserlight
Yes, that is the approach that I would have taken

Let's take another look at the condition of your original for loop:
Code:
`for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i){`
What it says is this: loop from the first character until a character that is not in the range '0' to '9' is found. This works great if your input consists entirely of digits from '0' to '9', e.g., "123" as you would loop over the entire input string. But suppose your input consists of the non-decimal range of hexadecimal digits, e.g., "ABC". Your loop will stop immediately because 'A' is a character that is not in the range '0' to '9'. Therefore the result is 0, which is obviously wrong.

Compounding this problem, within the loop body, you check to see if the character is a valid hex digit in the sense of whether it is a digit that is among the alphabetic hexadecimal digits 'A' to 'F'. Of course, knowing that within the loop body the character is surely in the range '0' to '9', it follows that it is surely "not a valid hex digit", therefore 0 is returned. Consequently, your function always returns 0, because either the character is not in the range of '0' to 9' hence 0 is returned, or it is in the range of '0' to '9' but not in the range of 'A' to 'F' (which is always true), hence 0 is returned.
That makes sense. Also, would it have been easier to run the program with an infinite while loop like

Code:
```while(1){
// do code here
}```

2. Originally Posted by KingFlippyNips
would it have been easier to run the program with an infinite while loop
No, because you are looping over the characters one by one until the end of the string, hence a for loop that tests for the null character in the condition is a natural way of expressing the loop.