#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    How to convert ASCII to decimal?


    Hey guys

    First of all i want to say that I am not a experienced programmer. I have tried to make a function that converts a ASCII string to a value.

    ex.
    ASCII: 12345

    and get transfered to a int so that the int has the value 12345.

    Anyone knows how it's done?

    :confused:
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    sscanf(str, "%d", &intval);

    Comments on this post

    • FnepZ agrees : Good advice.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    86
    Rep Power
    6
    u can also use the atoi function: link

    Comments on this post

    • FnepZ agrees
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    If you are reading that string in from the keyboard, then sscanf as Mitakeet described would be the way to go -- unless you're using iostream in C++, in which case it would be
    cin >> intval;

    If you already have the string, then either atoi (shorthand for "ASCII to integer") or strtol ("string to long"). atoi is most commonly used, but it has inherent problems in that you cannot tell whether it succeeds or not, plus it cannot handle overflow. strtol is more robust.

    To learn more, RTFM on all three functions.

    PS
    For a string that you already have, you can also use sscanf. Basically, it's the scanf that works on strings.

    Comments on this post

    • FnepZ agrees
    Last edited by dwise1_aol; May 24th, 2011 at 10:48 AM.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Strictly, that would be converting an ASCII representation of a decimal number to an int, not to decimal, the internal representation is binary. It is only a decimal if you choose to display it that way, and in that case it is a conversion from int to ASCII, which is what the %d format specifier does for example.

    Comments on this post

    • FnepZ agrees
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Thanks! :)


    Thank you all. I used atoi(). :p

IMN logo majestic logo threadwatch logo seochat tools logo