|
|
View previous topic :: View next topic |
Author |
Message |
KL
Joined: 24 Feb 2004 Posts: 11
|
Converting long int to string problem |
Posted: Tue Feb 24, 2004 4:08 pm |
|
|
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
|
|
Posted: Tue Feb 24, 2004 4:51 pm |
|
|
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
|
|
Posted: Wed Feb 25, 2004 8:03 am |
|
|
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. |
|
|
|
|
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
|