Thread: Converting hexidecimal digits to integer value

  1. #16
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33
    Quote Originally Posted by laserlight View Post
    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. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,149
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Digits to Words Help!
    By McZiploc in forum C Programming
    Replies: 6
    Last Post: 02-16-2013, 08:30 PM
  2. Sum of digits of an integer: Odd or Even?
    By Devolution in forum C Programming
    Replies: 14
    Last Post: 03-06-2009, 06:24 PM
  3. converting digits into words
    By nynicue in forum C Programming
    Replies: 12
    Last Post: 11-02-2008, 06:10 AM
  4. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  5. Replies: 1
    Last Post: 03-26-2006, 03:44 PM

Tags for this Thread