transfer from ASCII to int

Discussion in 'C Programming' started by QQ, Jun 8, 2005.

  1. QQ

    QQ Guest

    Hello I have a string
    "12345" for example,
    I'd like to change it to put the first n digits(123 for example) into
    int a=123,
    the 4,5 digits to be b=45.

    Now I am using atoi.
    I parse the string "12345" first to be s1,s2,
    and then change it to be a=atoi(s1) b=atoi(s2)
    Any better way for it?
    Thanks a lot!
    QQ, Jun 8, 2005
    #1
    1. Advertising

  2. QQ

    Michael Mair Guest

    QQ wrote:
    > Hello I have a string
    > "12345" for example,
    > I'd like to change it to put the first n digits(123 for example) into
    > int a=123,
    > the 4,5 digits to be b=45.
    >
    > Now I am using atoi.
    > I parse the string "12345" first to be s1,s2,
    > and then change it to be a=atoi(s1) b=atoi(s2)
    > Any better way for it?
    > Thanks a lot!


    Give us code, please.
    Your description is not as clear as well-written code and
    may hide errors.

    It is almost always a bad idea to use atoi() as there is
    an error-checking function which does the same job and
    reports errors: strtol()

    BTW: You need only to strcpy() once as the second atoi()
    /strtol() call can be passed str+n where str is a pointer
    to the string.

    If n is constant, you also can abuse sscanf() to perform
    the job:
    e.g. my_n = 3:
    ret = sscanf(str, "%3d%d", &a, &b);
    if (ret != 2) {
    /* maybe it is time for checking strlen(str) and some error
    ** handling */
    }
    (untested)
    or, with more flexibility and less readability
    #define MY_N 3
    #define stringize(s) #s
    #define XSTR(s) stringize(s)

    ....
    ret = sscanf(str, "%" XSTR(MY_N) "d%d", &a, &b);
    ....
    (untested, too)


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Jun 8, 2005
    #2
    1. Advertising

  3. QQ

    CBFalconer Guest

    Michael Mair wrote:
    > QQ wrote:
    >
    >> Hello I have a string "12345" for example,
    >> I'd like to change it to put the first n digits(123 for example)
    >> into int a=123, the 4,5 digits to be b=45.
    >>

    .... snip ...
    >
    > If n is constant, you also can abuse sscanf() to perform
    > the job:
    > e.g. my_n = 3:
    > ret = sscanf(str, "%3d%d", &a, &b);
    > if (ret != 2) {
    > /* maybe it is time for checking strlen(str) and some error
    > ** handling */
    > }
    > (untested)
    > or, with more flexibility and less readability
    > #define MY_N 3
    > #define stringize(s) #s
    > #define XSTR(s) stringize(s)
    >
    > ....
    > ret = sscanf(str, "%" XSTR(MY_N) "d%d", &a, &b);
    > ....
    > (untested, too)


    Why fool about with any flavor of scanf. Try (untested):

    /* a non-digit ends the field */
    unsigned int usefield(const char *s, int n)
    {
    unsigned int value = 0;

    while (n-- && isdigit((unsigned char)*s))
    value = 10 * value + (*s++ - '0');
    return value;
    } /* usefield untested */

    Don't forget to #include <ctype.h>. Now the OP can do:

    a = usefield(str, 3);
    b = usefield(str+3, 2);

    You can gussy it up to absorb leading blanks if you wish.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jun 9, 2005
    #3
  4. QQ

    Aaron Gage Guest

    QQ wrote:
    > Hello I have a string
    > "12345" for example,
    > I'd like to change it to put the first n digits(123 for example) into
    > int a=123,
    > the 4,5 digits to be b=45.
    >
    > Now I am using atoi.
    > I parse the string "12345" first to be s1,s2,
    > and then change it to be a=atoi(s1) b=atoi(s2)
    > Any better way for it?
    > Thanks a lot!
    >



    try (untested):

    int N = ??, i, a, b;

    char cpy[???];
    char input[???];

    gets(input);

    // copy 1st N chars
    strncpy(cpy, input, N);
    a = atoi(cpy);

    // copy remaining chars
    strncpy(cpy, input+N, strlen(input)-N);
    b = atoi(cpy);
    Aaron Gage, Jun 10, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,195
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,612
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    429
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    931
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    417
    James Kanze
    Apr 17, 2007
Loading...

Share This Page