Thread: Problem with atoi in C

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    Problem with atoi in C

    Hello all,

    I am working on a program on a PIC18 processor that takes and inputed character string and is supposed to return a two digit year from an RS232 read. Here is the code and the calling function:

    void read_rs232_date(struct datetime* input_datetime) {
    char string_datetime[12];
    char datetime_number[2];
    unsigned int32 leap_year_determinator;

    rs232_read_successful = TRUE;

    get_string(string_datetime, 13);

    datetime_number[0] = string_datetime[8];
    datetime_number[1] = string_datetime[9];

    input_datetime->year2digit = atoi(datetime_number);

    The problem is with the atoi function, it is supposed to return 04, but it keeps returning 45 or 46. The really weird thing is that it works fine until the system clock reads 10pm. Before 22:00:00 the clock is fine and the atoi function returns the proper 2 digit year, after 22:00:00, it returns the 45 or 46 when the inputted string is 04.

    What am I doing wrong?

    Here is the code for the atoi function:

    //////////////////////////////////////////////////////////
    // This function is copied from standard lib //
    //////////////////////////////////////////////////////////
    int8 atoi(char *s) {
    int8 result;
    int8 base, index;
    char c;

    index = 0;
    base = 10;
    result = 0;

    // Omit all preceeding alpha characters
    if(s)
    c = s[index++];


    if (c >= '0' && c <= '9') {

    // Check for hexa number
    if (c == '0' && (s[index] == 'x' || s[index] == 'X')) {
    base = 16;
    index++;
    c = s[index++];
    }

    // The number is a decimal number
    if (base == 10) {
    while (c >= '0' && c <= '9') {
    result = 10*result + (c - '0');
    c = s[index++];
    }
    } else if (base == 16) { // The number is a hexa number
    while (c = TOUPPER(c), (c >= '0' && c <= '9') || (c >= 'A' && c<='F')) {
    if (c >= '0' && c <= '9')
    result = (result << 4) + (c - '0');
    else
    result = (result << 4) + (c - 'A' + 10);

    c = s[index++];
    }
    }
    }


    return(result);
    }

  2. #2

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Hello all,

    I am working on a program on a PIC18 processor that takes and inputed character string and is supposed to return a two digit year from an RS232 read. Here is the code and the calling function:

    Code:
    void read_rs232_date(struct datetime* input_datetime) {
    char string_datetime[12];
    char datetime_number[2];
    unsigned int32 leap_year_determinator;
    
    rs232_read_successful = TRUE;
    
    get_string(string_datetime, 13);
    
    datetime_number[0] = string_datetime[8];
    datetime_number[1] = string_datetime[9];
    The problem is with the atoi function, it is supposed to return 04, but it keeps returning 45 or 46. The really weird thing is that it works fine until the system clock reads 10pm. Before 22:00:00 the clock is fine and the atoi function returns the proper 2 digit year, after 22:00:00, it returns the 45 or 46 when the inputted string is 04.

    What am I doing wrong?

    Here is the code for the atoi function:

    Code:
    //////////////////////////////////////////////////////////
    // This function is copied from standard lib //
    //////////////////////////////////////////////////////////
    int8 atoi(char *s) {
    
    int8 result; int8 base, index; char c;
    index = 0; base = 10; result = 0; // Omit all preceeding alpha characters if(s) c = s[index++];
    if (c >= '0' && c <= '9') { // Check for hexa number if (c == '0' && (s[index] == 'x' || s[index] == 'X')) { base = 16; index++; c = s[index++]; }
    // The number is a decimal number if (base == 10) {
    while (c >= '0' && c <= '9') { result = 10*result + (c - '0'); c = s[index++]; }
    } else if (base == 16) { // The number is a hexa number
    while (c = TOUPPER(c), (c >= '0' && c <= '9') || (c >= 'A' && c<='F')) { if (c >= '0' && c <= '9') result = (result << 4) + (c - '0'); else result = (result << 4) + (c - 'A' + 10); c = s[index++]; }
    } }
    return(result); }

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    atoi() expects a string, but you're trying to pass it an array of 2 characters. datetime_number needs to be an array that's 3 elements wide, not 2. Then you need to do datetime_number[2] = '\0';

    I also don't know what get_string() is. Have you tried printing string_datetime directly to see if it's correct?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    FYI: This has nothing to do with your problem, but it's a bug waiting to rear its ugly head after you have "finished" the code:

    Code:
       char c;
    
       index = 0;
       base = 10;
       result = 0;
    
       // Omit all preceeding alpha characters
       if(s)
          c = s[index++];
    If s is null, you don't assign any value to c. Then you proceed to do lots of things involving c and s[index].

    Regards,

    Dave

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    itsme86 and Dave,

    Thanks for the tips and I will plug the potential hole with the c as well. Sending the \0 worked and now the datetime issue is resolved!

    Have you tried printing string_datetime directly to see if it's correct? Yes I did this using printf, i saw the input going into the atoi was correct and the output coming out was flawed, so I knew I had to fix either the input or the function.

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Problem with atoi()?
    By ruthgrin in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2006, 12:25 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM