Solved

Fill out binary string with zero's

Posted on 2011-03-08
8
236 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
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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

860 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