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

convert integer to string

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







convert integer to string
PostPosted: Wed Apr 30, 2008 9:24 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 30, 2008 10:15 pm     Reply with quote

The test program shown below displays this output:
Quote:
000002008


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

View user's profile Send private message Visit poster's website

PostPosted: Fri Aug 20, 2010 5:59 pm     Reply with quote

I was looking for exactly this. Thank you so much!
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Sat Aug 21, 2010 4:30 am     Reply with quote

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

View user's profile Send private message Visit poster's website Yahoo Messenger

PostPosted: Fri Dec 23, 2011 10:17 pm     Reply with quote

Thanku Very Happy
_________________
the more i think the confused i get
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