Solved

pad 8 digit binary number representations - in C#

Posted on 2012-04-05
5
398 Views
Last Modified: 2012-04-05
string binValue = Convert.ToString(64, 2);

But I need to show all 8 digits.

So

  string binValue = Convert.ToString(6, 2);

which would normally be "110" would need to be shown as:

00000110


How?

Thanks!!!  :)
0
Comment
Question by:knowlton
  • 2
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37813730
           var binStr = Convert.ToString(2, 2);
            
            var sb = new StringBuilder();
            for(int i=0;i<8-binStr.Length;i++)
            {
                sb.Append("0");
            }

            sb.Append(binStr);

Open in new window

0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 37813749
... is what I ended up doing as well.

Thanks,

Tom

Q:  .NET does not provide a cleaner way to do this?

Something like, "I am representing this value as an 8-bit binary.  I want each digit to be represented, even if it is OFF (1 would be "00000001", 64 would be "10000000")"

Without having to "brute force" pad the difference with "0"?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37813925
well this is default behaviour, any leading zeros are ripped off....
0
 
LVL 5

Author Comment

by:knowlton
ID: 37814075
ok

:)
0
 
LVL 40
ID: 37814302
string binValue = Convert.ToString ( 6, 2 ).PadLeft ( 8,'0' );
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

21 Experts available now in Live!

Get 1:1 Help Now