[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# pad 8 digit binary number representations - in C#

Posted on 2012-04-05
Medium Priority
419 Views
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
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
• 2
• 2

LVL 20

Accepted Solution

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);
``````
0

LVL 5

Author Closing Comment

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

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

LVL 5

Author Comment

ID: 37814075
ok

:)
0

LVL 40

Expert Comment

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

## Featured Post

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
###### Suggested Courses
Course of the Month15 days, left to enroll