Solved

pad 8 digit binary number representations - in C#

Posted on 2012-04-05
5
409 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:Tom Knowlton
[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
  • 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:Tom 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:Tom Knowlton
ID: 37814075
ok

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

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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