Thread: String to Numeric Value

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    15

    String to Numeric Value

    I am looking to convert a string (e.g. "F8A2" or "-927") into its numeric value. I figure I need to do it one char at a time but how does "A" become "10" and "9" become the value 9. I need to do math on these characters so they need to become numbers. Is there a function for this or do I need to create a huge switch loop that sets each valid decimal or hexadecimal character into another int variable.

    Thanks in advance.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Look at this chart and you should be able to see how the math works.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Yah I've been looking at the ASCII table but "2" + "7" should equal 9 not 105 (50 + 55). I need "B" to equal 11 and "F" to equal 15, etc. Create my own lookup in the c program???

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That looks like hex (hexadecimal) to me:

    0-F: 0123456789ABCDEF

    This is base 16, so numbers in it are valued 0 through 15, for each of them. The letters are just larger numbers here. Don't think of them as letters with hex.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by rwagstaff View Post
    Yah I've been looking at the ASCII table but "2" + "7" should equal 9 not 105 (50 + 55).
    But '2' = 50 and '7' = 55. Why wouldn't it be 105?

    If '2' = 50, what do you have to do to actually make it 2?

    Quote Originally Posted by rwagstaff View Post
    I need "B" to equal 11 and "F" to equal 15, etc. Create my own lookup in the c program???
    That's not a bad way to go...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by rwagstaff View Post
    Yah I've been looking at the ASCII table but "2" + "7" should equal 9 not 105 (50 + 55). I need "B" to equal 11 and "F" to equal 15, etc. Create my own lookup in the c program???
    Don't add chars directly there. Instead, use the fact that the digits are consecutive. I.e.
    '2' - '0' == 2 and,
    '7' - '0' == 7 and,
    '9' - '0' == 9
    so
    ('2' - '0') + ('7' - '0') == 2 + 7 and '9' - '0' == 9
    therefore
    '2' - '0' + '7' - '0' == '9' - '0'
    Since we want the result of '9', rearrange like so:
    '2' - '0' + '7' - '0' + '0' == '9'
    Then finally, cancelations and we get:
    '2' + '7' - '0' == '9'

    To use 'A' to 'F', also use the fact that the letters are consecutive.
    'A' - 'A' == 0
    'F' - 'A' == 5
    However, we want 'A' to produce 10 and 'F' to produce 15. No problem, just detect letters and add 10.

    So, if it's a letter, subtract 'A' and add 10, but if it's a digit, just subtract '0'.
    You don't need to care what the ASCII values are at any point for this.
    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"

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    One could also use the strtol() function, which is declared in <stdlib.h>.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by grumpy View Post
    One could also use the strtol() function, which is declared in <stdlib.h>.
    Why? You don't learn anything about doing the conversions that way.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by WaltP
    Why? You don't learn anything about doing the conversions that way.
    Because you avoid reinventing the wheel and avoid mistakes in your own implementation, so you should use it besides learning to do without it.
    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

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Thanks for all your help people. I ended up just creating a function that accepts a char and returns an int. Switch statement with every option for 0-9 and AaBbCcDdEeFf. Pain in the ass but I guess C doesn't have a built in function. Strtol() maybe does the job but didn't try it. I love C...its like going to the dentist. =)

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by rwagstaff View Post
    Thanks for all your help people. I ended up just creating a function that accepts a char and returns an int. Switch statement with every option for 0-9 and AaBbCcDdEeFf. Pain in the ass but I guess C doesn't have a built in function. Strtol() maybe does the job but didn't try it. I love C...its like going to the dentist. =)
    I'm just amazed you didn't use a hex printf() format when you needed to, and a decimal format, whenever you needed to.


    Code:
    #include <stdio.h>
    
    int main (void) {
    
       int i=152;
    
       printf("i in hex: %x   i in decimal: %d\n",i, i); //use %X for cap letters in hex
    
       return 0;
    }
    Maybe I didn't understand your problem, however.

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by laserlight View Post
    Because you avoid reinventing the wheel and avoid mistakes in your own implementation, so you should use it besides learning to do without it.
    And when you get to the next language that doesn't have an strtol-like function, you are a cripple. C is not the only language out there. and presumably is not the last one to be learned.

    I've never had a function like that in any other language I've used, and I have never used that function in over 25 years of professional C work. Knowing how to do something has helped me greatly.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Well I am just a noob trying to learn how to program. I appreciate your help everyone. Please review the function below and discuss my ineptness. Thanks a bunch!! =)

    Code:
    int HexValue(char c)
    {
        switch(c)
        {
            case 'A':
                return 10;
                break;
            case 'a':
                return 10;
                break;
            case 'B':
                return 11;
                break;
            case 'b':
                return 11;
                break;
            case 'C':
                return 12;
                break;
            case 'c':
                return 12;
                break;
            case 'D':
                return 13;
                break;
            case 'd':
                return 13;
                break;
            case 'E':
                return 14;
                break;
            case 'e':
                return 14;
                break;
            case 'F':
                return 15;
                break;
            case 'f':
                return 15;
                break;
            case '0':
                return 0;
                break;
            case '1':
                return 1;
                break;
            case '2':
                return 2;
                break;
            case '3':
                return 3;
                break;
            case '4':
                return 4;
                break;
            case '5':
                return 5;
                break;
            case '6':
                return 6;
                break;
            case '7':
                return 7;
                break;
            case '8':
                return 8;
                break;
            case '9':
                return 9;
                break;
            default :
                return 0;
        }
    }
    And here would be the calling statement which would result in num being equal to 27. (15 + 12)

    Code:
    int num;
    num = HexValue("F") + HexValue("C");

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Why does it number the code with strange numbers and put a huge amount of blank white space at the bottom when I submit code blocks???? Anyone??? I'm a newbie. =)

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by rwagstaff View Post
    Why does it number the code with strange numbers and put a huge amount of blank white space at the bottom when I submit code blocks???? Anyone??? I'm a newbie. =)
    I'm floored that you don't want to use the hex format C has built into printf() for you. Why?

    "F" is a string with two char's - an 'F' char (note the single quotation mark on each side, not a double), and an end of string char: '\0'.

    Use a char, not a string, to compare to a char in your switch test.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-09-2012, 10:30 PM
  2. alpha-numeric to numeric program
    By johnhuge in forum C Programming
    Replies: 2
    Last Post: 03-24-2012, 12:08 AM
  3. C++: Converting Numeric String to Alpha String
    By JosephCardsFan in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 07:07 AM
  4. Numeric to string help
    By StupidQuestion in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2003, 12:56 PM
  5. Is there a function to convert a numeric value into a string?
    By sundeeptuteja in forum C Programming
    Replies: 6
    Last Post: 11-06-2002, 02:11 PM