?
Solved

pad 8 digit binary number representations - in C#

Posted on 2012-04-05
5
Medium Priority
?
414 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 2000 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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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