Solved

pad 8 digit binary number representations - in C#

Posted on 2012-04-05
5
394 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

13 Experts available now in Live!

Get 1:1 Help Now