[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Fill out binary string with zero's

Posted on 2011-03-08
8
Medium Priority
?
243 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
7 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
Industry Leaders: 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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