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

Converting long int to string problem

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



Joined: 24 Feb 2004
Posts: 11

View user's profile Send private message

Converting long int to string problem
PostPosted: Tue Feb 24, 2004 4:08 pm     Reply with quote

I'm attempting to use "sprintf" to convert a long int to a string. Here is a segment of the code.

si_equiv = (long int)(125.0 * 16.018463);
sprintf(PR_SI_Units,"\%ld",si_equiv); // convert to string
printf("si equiv = %lu\n\r", si_equiv);
printf("si string = %s\n\r", PR_SI_Units);
printf("si float = %5.1f\n\r", atof(PR_SI_Units));


The output results are:

si equiv = 2002
si string = 002
si float = .0

I reviewed the sprintf function in the manual as well as past postings on this subject but I must be missing something. Could someone tell me what I am missing?

Thanks,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 24, 2004 4:51 pm     Reply with quote

Using PCM vs. 3.184, with the test program shown below, I got
these results:

si equiv = 2002
si string = 2002
si float = 2001.9
Code:
#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <stdlib.h>
//===============================
main()
{
int16 si_equiv;
char PR_SI_Units[10];

si_equiv = (long int)(125.0 * 16.018463);
sprintf(PR_SI_Units,"\%ld",si_equiv); // convert to string
printf("si equiv = %lu\n\r", si_equiv);
printf("si string = %s\n\r", PR_SI_Units);
printf("si float = %5.1f\n\r", atof(PR_SI_Units));
while(1);
}
KL



Joined: 24 Feb 2004
Posts: 11

View user's profile Send private message

PostPosted: Wed Feb 25, 2004 8:03 am     Reply with quote

PCM Programmer,

Thanks for the reply. I'm using version 3.157 and a PIC18F6720. I attempted to run your code and I still receive the same results as before. It must be a compiler problem with the 3.157 version.
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