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

Weird atof() conversion

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








Weird atof() conversion
PostPosted: Tue Feb 21, 2006 4:57 am     Reply with quote

Hi all,
i have some problem converting string to long and unable to figure it out. My program receives reads about 21 characters on the RS-232 and selects all the digits in it, stores it in another array and then convert it into long. If the digits are ABC in variable 'digitsrx' then after conversion prints 0BC. Can u suggest a possible solution, why this doesnt convert this SIMPLE string.
Following is the code:


Code:

#include <16f877a.h>
#device *=16
#include<stdlib.h>
//#include<input.c>
#fuses  XT,NOWDT,NOLVP,NOPROTECT,BROWNOUT //--> try these also
#use delay (clock=4000000,restart_wdt)
#use rs232(baud=4800,xmit=PIN_C6,rcv=PIN_C7,stream=DEV,ERRORS)
#include<lcd.c>

long feet=0;
long feeta=0;
char digitsrx[4]={0};
int i=0,j=0;
char rxdata[21]={0};
char endptr[4];

void main()
{
    lcd_init();
    lcd_gotoxy(1,1);

    while(1)
    {
        fgets(rxdata,DEV);
 
        j=0;               
        for(i=0;i<21;i++)
        {
            if(isdigit(rxdata[i]))
            {
                digitsrx[j]=rxdata[i];
                j++;
            }
        }
        digitsrx[j]='\0';  // terminating with NULL       

        for(i=0; i<21;i++)
            rxdata[i]=0;
       
        lcd_gotoxy(1,1);               
        printf(lcd_putc,"%s",digitsrx); // correct result printed

      feet=strtod(digitsrx,endptr); // converted ascii to long
      lcd_gotoxy(1,2);
      printf(lcd_putc,"%lu",feet);   // wrong result
      feeta=atol(digitsrx);
      lcd_gotoxy(10,2);
      printf(lcd_putc,"%lu",feeta);   //wrong result

       

    }
}
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Tue Feb 21, 2006 9:01 am     Reply with quote

So if your first loop finds more than 3 digits, what happens? Your array
Code:
char digitsrx[4]={0};
will fall apart!

This may not be the ultimate problem but you definately will have an issue later.

By the way, what is your compiler version number? There is a history of funny behavior with some of the built-in functions.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
KenMacfarlane



Joined: 20 Sep 2005
Posts: 23
Location: Glasgow, Scotland, UK

View user's profile Send private message

Me too!
PostPosted: Tue Mar 07, 2006 4:38 am     Reply with quote

I'm having similar problems with pcwh v3.235 on an 18f6621 (yes, I know that its not the latest, but the latest contains a bug that I'm waiting for CCS to fix):

Code:

sprintf(pszTmp, "1234567"); // crashes too: Resets to start of main
fPart2 = strtod(pszTmp, &remptr);
dwPart2 = (int32)fPart2
// strtod also crashes with 8 digit null term'd str 34628712


Incidentally, is there code for a 32 bit version of strtoul around? That's what I really want - the above code, including strtod, could be replaced by such a function.
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