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

atoi32 not working

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



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

atoi32 not working
PostPosted: Wed Aug 12, 2009 8:35 am     Reply with quote

I am receiving gps coords in a pic18f252 and using 4.092.

The ascii longitude looks like:

"09744.6622" and is assigned to a string called gps.

I then get rid of the decimal point and assign the new string to cal:

"097446622"

I then run the atoi32(cal) and my result is 13560542

Hmmmm????

Anyone have any ideas?
Ttelmah
Guest







PostPosted: Wed Aug 12, 2009 9:04 am     Reply with quote

When you generated your new string, did you remember to put a 'null' character into the location after the last '2'?.
If not, atoi32, will carry on walking through memory till it does find a '\0' character, and what you get will depend on what is in these latter characters...

Best Wishes
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

Re: atoi32 not working
PostPosted: Wed Aug 12, 2009 9:13 am     Reply with quote

I have 4.064 and I used MPLAB SIM under DEBUGGER to create this test program:

Code:

#include <18F252.h>

#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)

char c[] = "097446622" ;
int32 result;

#include <stdlib.h>
//=========================
void main()
{


result =  atoi32(c);

delay_ms(1);  //BREAKPOINT SET HERE

}



Using the WATCH option I get:

c = 0x3930 (14640 in decimal in the next column over)
result = 0x5CEEADE (97446622 in decimal in the next colum over)

so it looks like everything is okay.
ljbeng



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 9:29 am     Reply with quote

Yes I have a null character as the last character. I don't see any differences from mkuang and my code. I am viewing as decimal. Latitude is coming out ok with the same exact code as longitude. I will keep looking.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 12, 2009 9:32 am     Reply with quote

Ttelmah wrote:
When you generated your new string, did you remember to put a 'null' character into the location after the last '2'?.
If not, atoi32, will carry on walking through memory till it does find a '\0' character, and what you get will depend on what is in these latter characters...

Best Wishes


When you declare a string like my_string = "xxxxxxx" doesn't the compiler automatically put a null termination character at the end of the string?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 10:32 am     Reply with quote

Quote:
I don't see any differences from mkuang and my code.

Post your short, compilable test program that shows the problem.
ljbeng



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 10:55 am     Reply with quote

A short compilable program will be difficult. I should mention that the exact same program running with V2.49 works just fine. Another guy took my program and is using the new V4 compiler and he is seeing the problem.

In the receive interrupt when I get to the longitude data, I run the following code:

Code:


   if ((a == 5) && (b !=',')){

       gps[chrcnt]=b;
       if (chrcnt == 9)
       {
         chrcnt++;     // get rover longitude
         gps[chrcnt] = 0;
//gps looks like "09744.6622"

           cal[0] = gps[0];
           cal[1] = gps[1];
           cal[2] = gps[2];
           cal[3] = gps[3];
           cal[4] = gps[4];
           cal[5] = gps[6];
           cal[6] = gps[7];
           cal[7] = gps[8];
           cal[8] = gps[9];
           cal[9] = 0;
//call looks like "097446622"

           roverlon = atoi32(cal);
//roverlon = 13560542

       }
       chrcnt++;
   }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 11:10 am     Reply with quote

But you could make that into a test program similar to mkuang's fairly
easily. Show the declarations of 'cal' and 'roverlon'.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 12, 2009 11:24 am     Reply with quote

Well, if you use this test program you get the same result as before:

Code:

#include <18F252.h>

#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)

char cal[10];
char gps[] = "09744.6622";

int32 roverlon;

#include <stdlib.h>

void main(void)
{
        cal[0] = gps[0];
           cal[1] = gps[1];
           cal[2] = gps[2];
           cal[3] = gps[3];
           cal[4] = gps[4];
           cal[5] = gps[6];
           cal[6] = gps[7];
           cal[7] = gps[8];
           cal[8] = gps[9];
           cal[9] = 0;

           roverlon = atoi32(cal);
      
           delay_ms(1); //BREAKPOINT HERE

}


Using the WATCH option I get roverlon = 9744622. I would check to make sure you declared roverlon and cal correctly.
ljbeng



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 2:51 pm     Reply with quote

Sorry, I meant V3.249 seems to work ok.

I just added this to my code:

Code:

sprintf(cal,"97446622");
roverlon = atoi32(cal);


and I get the same wrong answer.
Ttelmah
Guest







PostPosted: Wed Aug 12, 2009 3:29 pm     Reply with quote

The source code for atoi32, is sitting accessible to you, in stdlib.h.
Now, things that could cause problems:
1) Remember it returns a signed int32, not an unsigned. Should only 'matter' if the value goes beyond 2^31.
2) It _will_ go wrong, on later compilers, if you have changed the default 'long' type (won't apply to any of the posted examples so far).

How is your watch setup?. The value you are seeing is exactly what you would expect if you were looking at three bytes only of the result. Have you actually tried printf and outputting the value?.

Best Wishes
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 12, 2009 3:37 pm     Reply with quote

ljbeng wrote:
Sorry, I meant V3.249 seems to work ok.

I just added this to my code:

Code:

sprintf(cal,"97446622");
roverlon = atoi32(cal);


and I get the same wrong answer.

Why don't you take my short program above and compile it using V3.249 and 4.092, look at the result in WATCH and see what you see. It shouldn't take more than 2 minutes, instead of trying to guess where the problem is.
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