[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Fill out binary string with zero's

Posted on 2011-03-08
8
Medium Priority
?
241 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 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 32

Accepted Solution

by:
phoffric earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

650 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