Thread: strtol() or something like that?!?!

  1. #1
    Registered User
    Join Date
    Jan 2013

    strtol() or something like that?!?!

    Hi at all,
    I have to convert a characters into an Integer.
    I received via TCP " INTERVAL 500" and the Timer interval has to be set to 500, for example. I thought strtol() will solve my problem.

    char *payload = "INTERVAL 500";
    int tinterval = 0;
    inteval = strtol(payload,NULL,10);

    But this dosen't work, because there are nonconvertible chars at the beginning.
    Can someone give me a hint to solve my problem? I have no idea at the moment.

  2. #2
    Registered User
    Join Date
    Jun 2011
    sscanf() perhaps?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Well, it will depend a bit on your payload, and what all the possible formats for it are, and what types of data you want to extract.

    If the payload always ends with a space followed by the number you want, and nothing more, you could use strrchr to find the last space in the string (the extra r tells it to start at the end and work backward). Then, once you find the last space, use strtol starting at the next char. E.g.:
    p = strrchr(payload, ' ');
    if (p)
        interval = strtol(p+1, NULL, 10);
    Alternatively, you can use strpbrk and pass it all the digit characters, to find the first digit in your payload, and use strtol on that.

    Alternatively, you can use strtok to split up your payload in some sensible manner (split on spaces?), and use the right part of the string that way.

    Alternatively, you could use sscanf to scan the payload for your data, something like
    if (sscanf(payload, "INTERVAL %d", &interval) == 1)
        // found it, do something with interval
        // didnt find it, maybe error
    There are probably several other ways you could do it. If all the possible formats are "WORD ###", I would probably use strpbrk or strrchr for simplicity. strpbrk may be a bit more robust, since it will always tell you if it found digits, where as strrchr may find the last space, but after that may be another word (e.g. if you get bad data).

    EDIT: It's not clear, from your post, whether there are other payloads you need to process, with other data besides interval (e.g. "LENGTH 500"). If you only have to deal with interval, sscanf may be easiest. If you have many different payload types, sscanf is probably not best/easiest.
    Last edited by anduril462; 01-16-2013 at 01:50 PM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    I second Matticus's sscanf suggestion. Example:

    char *payload = "INTERVAL 500";
    int interval;
    char end;
    if (sscanf(payload, " INTERVAL %d %c", &interval, &end) != 1) {
        printf("Unrecognized payload string");
    printf("found interval: %d\n", interval);
    1. leading whitespace on the scanf format ignores any initial whitespace
    2. whitespace after the %d in the scanf format ignores trailing whitespace
    3. Return value should be 1. The %c at the very end looks for garbage characters and will cause a return value of 2 if scanf gets that far - e.g. string like " INTERVAL 500 XXX" would then be detected as invalid

  5. #5
    Registered User
    Join Date
    Jul 2012
    Will try it with strpbrk ().
    In this section of code payload can only be "INTERVAL ###".


  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Nice, France
    Second account !! Not good :/
    Code - functions and small libraries I use

    It’s 2014 and I still use printf() for debugging.

    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtol() / malloc
    By sniperwire in forum C Programming
    Replies: 3
    Last Post: 11-02-2008, 09:01 PM
  2. strtol
    By kiz in forum C Programming
    Replies: 9
    Last Post: 10-21-2007, 11:42 AM
  3. strtol() question
    By movl0x1 in forum C Programming
    Replies: 13
    Last Post: 05-17-2007, 10:26 AM
  4. strtol() is the only way?
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 06-07-2006, 02:32 PM
  5. someone help? strtol?
    By salsa in forum C Programming
    Replies: 4
    Last Post: 10-03-2004, 06:34 AM