View previous topic :: View next topic |
Author |
Message |
User Guest
|
convert integer to string |
Posted: Wed Apr 30, 2008 9:24 pm |
|
|
hi, Any one done any form to convert long integer 32 bits in formated string with 'n' zeros left ?
ccs have built in function sprint to do convert (ie)
char str[ 9 ] = "00000000"; //initial value of string 8 zeros
int32 x = 0x07D8 //initial value of integer 2008
sprintf(str, "%lu", x); //result "2008"
the result is correct in string str, but i wold like and need to result "000002008" with n zeros to left.
any one have idea to do is ??
best regards, Renato |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 30, 2008 10:15 pm |
|
|
The test program shown below displays this output:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//====================================
void main()
{
char str[20];
int32 x = 2008;
sprintf(str, "%09lu", x);
printf(str);
while(1);
} |
This was tested with compiler vs. 4.071. |
|
|
Democrito
Joined: 20 Aug 2010 Posts: 2 Location: Tibet
|
|
Posted: Fri Aug 20, 2010 5:59 pm |
|
|
I was looking for exactly this. Thank you so much! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat Aug 21, 2010 4:30 am |
|
|
and of course, note carefully, that PCMprogrammer made the string longer, since your required output (9 characters), requires a minimum of _10_ characters of storage for the string.
Best Wishes |
|
|
sumesh89
Joined: 17 Dec 2011 Posts: 2 Location: Kerala
|
|
Posted: Fri Dec 23, 2011 10:17 pm |
|
|
Thanku _________________ the more i think the confused i get |
|
|
|