Thread: hex to dec question

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    4

    hex to dec question

    Code:
    int htoi(char s[])
    
    {
        int i = 0; /* Iterate over s */
        int n = 0; /* Built up number */
    
     
        /* Remove "0x" or "0X" */
        if ( s[0] == '0' && s[1] == 'x' || s[1] == 'X' )
            i = 2;
     
        while ( s[i] != '\0' )
        {
            int t;
     
            if ( s[i] >= 'A' && s[i] <= 'F' )
    
                t = s[i] - 'A' + 10;
    
            else if ( s[i] >= 'a' && s[i] <= 'f' )
                t = s[i] - 'a' + 10;
            else if ( s[i] >= '0' && s[i] <= '9' )
                t = s[i] - '0';
            else
                return n;
     
            n = 16 * n + t;
            ++i;
        }
     
        return n;
    
    }
    Hi, I'm working through the problems in the K&R C book. Anyway I've read around and I can't understand this particular line?

    Code:
    t = s[i] - 'A' + 10;
    I don't understand why the 10 is being added. Could anyone please explain?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Why is 'A' is subtracted from s[i]? Could you please explain?
    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

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    EDIT: Ok so I think I understand it. A = 10 in hex, B = 11 ...F = 16. The reason the numeric value of A is being substracted and then 10 added to it is so that the number can be repesented by it's hex value. For example:

    If s[i] = 'B' which has a deciman value of 66. Then

    t = s[i] - 'A' + 10;


    will equal to t = 66 - 65 + 10 which is 11.
    11 is the hex value of B.
    Last edited by rbz; 08-23-2012 at 12:02 PM.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by rbz View Post
    EDIT: Ok so I think I understand it.
    That's exactly it. As you can see it's only done if s[i] is between 'A' and 'F' or 'a' and 'f'. For all other cases '0' is subtracted since 0-9 is covered in the decimal set.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I know this is not very common, but I always write it like this:
    Code:
    BinVal = AsciiVal - 'A' + 0xA;
    This to me makes it clear that you're moving a value from being based at 'A' to being based at 0xA, and feels somewhat like you're removing a magic number too.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. java question, how do you interpret this question.
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-02-2006, 10:30 AM
  5. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM