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 support@ccsinfo.com

An atol problem

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



Joined: 04 Jul 2014
Posts: 44

View user's profile Send private message

An atol problem
PostPosted: Fri Jul 04, 2014 11:25 am     Reply with quote

Hi guys. I have an atol problem. I am using pic16f877a. I am sending data via pc.
Code:

Unsigned long x;
Char data [20]={};
For (I=0, I <20, I++)     Data [I]=getc ();
X=atol (data [0]);

Lets say if data [0] is 9 I get on lcd 11.
Why it can be?
Ttelmah



Joined: 11 Mar 2010
Posts: 19267

View user's profile Send private message

PostPosted: Fri Jul 04, 2014 11:46 am     Reply with quote

Lots of potential things:

9 is not an ASCII number.
0x39 is the digit '9'.

However then remember that the string needs to be _terminated_ (`\0' character). Otherwise ATOL will keep walking through memory until it finds a terminator. You need to add a terminator after your last character. This is why a 10 character 'string' needs 11 characters of storage.
It is probably finding some much larger number like 3456780011, and only returning the '11' since it then overflows....

Syntax is also wrong.
You want &data[0], or just data passed to the function.

Then how is a 'long' (max 65536) going to hold a 20 digit number?....
Ruby



Joined: 04 Jul 2014
Posts: 44

View user's profile Send private message

PostPosted: Sat Jul 05, 2014 12:30 am     Reply with quote

Thanks for reply. The program is like this:

Code:
.....
char data[20]={}; // ==> Global
unsigned long x=0; // ==> Global
......

for (i=0;i<25;i++)
               {
                data[i]=getc();
                }
            printf(LCD_PUTC,"\fD0=%c D1=%c D2=%c \nD3=%c D4=%c ",data[0],data[1],data[2],data[3],data[4]);   //==> Correct
            delay_ms (1000);
            x=atol('9');
            printf(LCD_PUTC,"\fx=%lu ",x);   //==> I am getting 11
            delay_ms (1000);
            x=atol(data[1]);
           
            printf(LCD_PUTC,"\fD0=%lu ",x);  //==> I am getting 2011 doesn't matter what is data[1]
                        delay_ms (1000);
            x=atol(data[2])*100+10*atol(data[3])+atol(data[4]);
            printf(LCD_PUTC,"\fD0=%lu ",x); 
                        delay_ms (1000);

data[20] will be convert to 7 different int
As x=atol(data[0]) , y=atol(data[2])*100+10*atol(data[3])+atol(data[4]);
etc......
Ttelmah



Joined: 11 Mar 2010
Posts: 19267

View user's profile Send private message

PostPosted: Sat Jul 05, 2014 12:56 am     Reply with quote

Key point is understanding what a 'string' is.

In C, a 'string', is an array of text characters _terminated with a '\0' character.

The termination is critical.

Second is understanding the difference between a value, and it's _address_.

atol, expects to receive a pointer to an array of characters (it's address in memory). You are calling it with the value 0x39, which it then treats as such a pointer. So it goes walking off through memory, starting at address 0x39, and returns what it finds. Could be anything.....

Code:

   char temp_buff[10];
   int8 val=123;
   int16 x;

   sprintf(temp_buff,"%d",val);
   //At this point 'temp_buff' contains 0x31 0x32 0x33, 0
   //Note the '0' - sprintf, automatically terminates

   x=atol(temp_buff); //The name of an array, _is_ it's address
   //x is now 123
   //Or
 
   x=atol(&temp_buff[1]);
   //x is now 23 (because the scan started at the second character)

   temp_buff[0]='9';
   temp_buff[1]='\0';
   //At this point 'temp_buff' contains 0x39, 0, 0x33, 0
   x=atol(temp_buff);
   //x is now 9


This is really basic C. Some basic reading is needed about C strings, and C pointers.
Ruby



Joined: 04 Jul 2014
Posts: 44

View user's profile Send private message

PostPosted: Sun Jul 06, 2014 3:00 pm     Reply with quote

Thank you. Now is working...
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