Solved

Fill out binary string with zero's

Posted on 2011-03-08
8
232 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:Kdo
Kdo earned 250 total points
Comment Utility

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
Comment Utility
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
Comment Utility

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now