Solved

Fill out binary string with zero's

Posted on 2011-03-08
8
239 Views
Last Modified: 2012-05-11
my_itoa works fine, but a have to fill out string with zero's, any solutions ?

// Ludde
0
Comment
Question by:Ludde72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 250 total points
ID: 35069718

Hi Ludde,

  memset (StringBuffer, 0, length (StringBuffer));


length (StringBuffer) could be computed any of several different ways, depending on how StringBuffer is defined.


Good Luck,
Kent
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35070156
If you meant ASCII zero's, then you need single quotes around the 0 (like '0') to make it ASCII.

If you defined StringBuffer as:

    char StringBuffer[33];

then I would suggest for the length using sizeof:

    memset (StringBuffer, '0', sizeof(StringBuffer) );

and if you need a terminating null byte to turn it into a c-string, then add:
   StringBuffer[ sizeof(StringBuffer) - 1 ] = 0;

So, now you have all '0' followed by a 0.

Then you can change the StringBuffer array size, and StringBuffer will still be initialized similarly.
0
 

Author Comment

by:Ludde72
ID: 35070659

static unsigned int number = 256;
static char FW[16];


my_itoa( number, FW, 2 );             // FW = 100000000
memset (FW, '0', sizeof(FW) );       // FW = 0000000000000000
FW[ sizeof(FW) - 1 ] = 0;               // FW = 0000000000000000

// I expected                                 // FW = 0000000100000000




0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 35071310
Ok, here is a fixup.
static unsigned int number = 1024+256;
   static char FW[17];
   int len;
   int lenFW = sizeof(FW);
   int offset;

   _itoa( number, FW, 2 );          // FW = 100000000
   len = strlen(FW);
   offset = lenFW-1-len;
   memmove( FW+offset, FW, len );
   memset (FW, '0', offset ); 
   FW[ lenFW - 1 ] = 0;

Open in new window

0
 

Author Comment

by:Ludde72
ID: 35081254
Thanks, it works fine !
Are 1024+256 a test example ?
The input can be 0 to 65535.

Thanks
// Ludde
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35081831
>> Are 1024+256 a test example ?
Yes. I forgot to modify the comment // FW = ...
But this one case (and also 0xFFFF) worked for me.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 37419047
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question