CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with "atoi"

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
fbtech



Joined: 25 Mar 2015
Posts: 7

View user's profile Send private message

Problem with "atoi"
PostPosted: Thu Apr 30, 2015 5:42 am     Reply with quote

Hello,
I'm using "atoi" as follows:
Code:

#include <main.h>

char date_mday[2] = "", date_mon[2] = "", date_year[2] = "", date_hour[2] = "", date_min[2] = "", tm_sec[2] = "";
unsigned int date_mday_v = 0, date_mon_v = 0, date_year_v = 0, date_hour_v = 0, date_min_v = 0, tm_sec_v = 0;


void initisialisation()
{
   PORTA   = 0x00;
   PORTB   = 0x00;
   PORTC   = 0x00;

   TRISA   = 0xF7;
   TRISB   = 0x01;
   TRISC   = 0x8C;

   SETUP_ADC_PORTS(NO_ANALOGS);

}

void main()
{
   initisialisation();

   // badge_config();
   
    // COMMENT DATE
   char comment_date[17] = "16:30:50 30/14/15";

   date_hour[0] = comment_date[0];
   date_hour[1] = comment_date[1];
   date_hour_v = atoi(date_hour);
   printf("hour : %d \n\r",date_hour_v);

   date_min[0] = comment_date[3];
   date_min[1] = comment_date[4];
   date_min_v = atoi(date_min);
   printf("min : %d \n\r",date_min_v);

   tm_sec[0] = comment_date[6];
   tm_sec[1] = comment_date[7];
   tm_sec_v = atoi(tm_sec);
   printf("sec : %d \n\r",tm_sec_v);

   date_mday[0] = comment_date[9];
   date_mday[1] = comment_date[10];
   date_mday_v = atoi(date_mday);
   printf("day : %d \n\r",date_mday_v);

   date_mon[0] = comment_date[12];
   date_mon[1] = comment_date[13];
   date_mon_v = atoi(date_mon);
   printf("month : %d \n\r",date_mon_v);

   date_year[0] = comment_date[15];
   date_year[1] = comment_date[16];
   
   date_year_v = atoi(date_year);
   printf("year : %c%c \n\r",date_year[0],date_year[1]);
   printf("year : %d \n\r",date_year_v);
   
   delay_ms(200);

   while(true);

} //Main


The problem is that I have this result:
http://www.4shared.com/download/JtLKh36Ece/atoi_Problem.jpg?lgfp=3000

for the "year" the conversion is wrong.

I dont undestand the problem.

Thanks.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Apr 30, 2015 7:02 am     Reply with quote

from the CCS manual
re ATOI() INPUT...
Quote:

string is a pointer to a null terminated string of characters.


where is your NULL char ?? ie ascii Z terminator ?
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: Problem with "atoi"
PostPosted: Thu Apr 30, 2015 7:16 am     Reply with quote

fbtech wrote:
Hello,
I'm using "atoi" as follows:
Code:

#include <main.h>

char date_mday[2] = "", date_mon[2] = "", date_year[2] = "", date_hour[2] = "", date_min[2] = "", tm_sec[2] = "";
unsigned int date_mday_v = 0, date_mon_v = 0, date_year_v = 0, date_hour_v = 0, date_min_v = 0, tm_sec_v = 0;

...

   date_hour[0] = comment_date[0];
   date_hour[1] = comment_date[1];
   date_hour_v = atoi(date_hour);
   printf("hour : %d \n\r",date_hour_v);

...

   date_year[0] = comment_date[15];
   date_year[1] = comment_date[16];
   
   date_year_v = atoi(date_year);
   printf("year : %c%c \n\r",date_year[0],date_year[1]);
   printf("year : %d \n\r",date_year_v);

...



atoi() takes a null-terinated string as its input. When you intialise, you empty all the strings. That means all they have is a null (0) as their first character, the second character is undefined. You then overwrite the null with actual characters.

Think about your two character strings, and the way they are placed in memory. When you set the two characters of the hour, the null in the string after it in memory, the minutes, terminates the hour string and atoi() works. That happen for all the strings until you get to the last one, the year, as then there is no following null, and atoi() does the best it can, but clearly won't produce the right result. I am sure that if you repeat the same code, then the first time around you wil get the result you have now, but it'll be even more confused the second time as the nulls will have been replaced by numeric characters from the first time through.

To convert a two character string, you need three characters space, to allow for the terminating null.
fbtech



Joined: 25 Mar 2015
Posts: 7

View user's profile Send private message

PostPosted: Thu Apr 30, 2015 7:26 am     Reply with quote

Every thing is fine now.

BIG THANKS.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group