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

String format with leading zeros (0)...

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



Joined: 07 Sep 2007
Posts: 41

View user's profile Send private message

String format with leading zeros (0)...
PostPosted: Thu Aug 05, 2010 9:25 am     Reply with quote

Hi there, does anyone know how I can format a string with leading zeros:

Ex:
Code:

char a[200] = "Hi there";
char b[200] = "";

sprintf(b,"%021s",b);

//// Result : b = "0000000000000Hi there";


I can do it this way in a normal compiler (cygwin)...
But with CCS doesn't seem to work.

Thanks in advance.
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

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

Re: String format with leading zeros (0)...
PostPosted: Thu Aug 05, 2010 10:30 am     Reply with quote

How about using memset? If you already know how many zeros you want you could do something like:

Code:

memset(b, '0', 21);


And then use strcat to combine them

Code:

strcat (b, a);


Audi80 wrote:
Hi there, does anyone know how I can format a string with leading zeros:

Ex:
Code:

char a[200] = "Hi there";
char b[200] = "";

sprintf(b,"%021s",b);

//// Result : b = "0000000000000Hi there";


I can do it this way in a normal compiler (cygwin)...
But with CCS doesn't seem to work.

Thanks in advance.
Audi80



Joined: 07 Sep 2007
Posts: 41

View user's profile Send private message

PostPosted: Thu Aug 05, 2010 11:10 am     Reply with quote

Hi there, I cant use strcat because it concatenates two strings and the result would be:

000000000000000000000Hi There...

I made it the following way which is not the most elegant way to do it:
Code:

   static unsigned int i = 0,j=0;
   memset(auxCard,0,sizeof(auxCard));
   sprintf(auxCard,"0000000000000000000");
        j=strlen(cartao);
   for(i = 20;j>0;--i){
      auxCard[i] = cartao[--j];
   }
     return auxCard;

But... it works... Wink
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Aug 06, 2010 1:34 am     Reply with quote

Try

Code:

#define MAX_LEN 21

char a[200] = "Hi there";
char b[200];

memset(b, '0', MAX_LEN);  // This will not null terminate the string!
b[MAX_LEN] = '\0';
strcpy(b + MAX_LEN - strlen(a), a);


I havn't tested it but it should work.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Fri Aug 06, 2010 2:21 am     Reply with quote

The 'leading zeros', for a string, with the other C you have tried, is _not_ standard. If you read the K&R manuals, you will find that '0' as a modifier, specifically says "for numeric conversions". So CCS omitting this, is really 'correct'!...
I'd suggest:
Code:

char a[200] = "Hi there";
char b[200];
int8 ctr;
for (ctr=0;ctr<(21-strlen(a));ctr++) b[ctr]='0';
sprintf(b+ctr,"%s",a);


Best Wishes
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