43

I want to display one byte in textbox. Now I'm using:

Convert.ToString(MyVeryOwnByte, 2);

But when byte is has 0's at begining those 0's are being cut. Example:

MyVeryOwnByte = 00001110 // Texbox shows -> 1110
MyVeryOwnByte = 01010101 // Texbox shows -> 1010101
MyVeryOwnByte = 00000000 // Texbox shows -> <Empty>
MyVeryOwnByte = 00000001 // Texbox shows -> 1

I want to display all 8 digits.

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
Hooch
  • 28,817
  • 29
  • 102
  • 161
  • See: http://stackoverflow.com/questions/1644609/c-problem-with-byte and specifically http://stackoverflow.com/questions/1644609/c-problem-with-byte/1644666#1644666 – Jeff Yates Jan 28 '11 at 14:39
  • As the code in this question has already [been misunderstood by another user](http://stackoverflow.com/questions/22894695/preceding-0s-in-integer-value), it should be pointed out that `MyVeryOwnByte` is not actually a `byte` here, and that (if that is the actual C# code used) literals such as `01010101` are *decimal* numbers (that happen to consist of only zeroes and ones); the actual bits of the bytes making up those numbers look quite a bit different. – O. R. Mapper Apr 06 '14 at 14:10
  • @O.R.Mapper It is only "pseudocode". – Hooch Apr 06 '14 at 16:01
  • @Hooch: Ok, then only the author of the [other question](http://stackoverflow.com/questions/22894695/preceding-0s-in-integer-value) was possibly mistaken. – O. R. Mapper Apr 06 '14 at 16:23
  • Does this answer your question? [Convert an integer to a binary string with leading zeros](https://stackoverflow.com/questions/23905188/convert-an-integer-to-a-binary-string-with-leading-zeros) – Jason C Jun 05 '21 at 17:48

4 Answers4

75
Convert.ToString(MyVeryOwnByte, 2).PadLeft(8, '0');

This will fill the empty space to the left with '0' for a total of 8 characters in the string

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
WraithNath
  • 17,658
  • 10
  • 55
  • 82
13

How you do it depends on how you want your output to look.

If you just want "00011011", use a function like this:

static string Pad(byte b)
{
    return Convert.ToString(b, 2).PadLeft(8, '0');
}

If you want output like "00011011", use a function like this:

static string PadBold(byte b)
{
    string bin = Convert.ToString(b, 2);
    return new string('0', 8 - bin.Length) + "<b>" + bin + "</b>";
}

If you want output like "0001 1011", a function like this might be better:

static string PadNibble(byte b)
{
    return Int32.Parse(Convert.ToString(b, 2)).ToString("0000 0000");
}
Gabe
  • 84,912
  • 12
  • 139
  • 238
  • @Jeff: I posted this version because sometimes the `new string()` technique is useful. You didn't question Gregory's mention of `PadRight`, and that function doesn't even solve the problem. – Gabe Jan 28 '11 at 14:48
  • How I reacted to someone else's response is entirely irrelevant. I just thought it strange to see you downplay your answer by referring to a better solution that you didn't give and that the suggestion you had was not particular easy to read. As SO is a resource for getting a good answer, you'd be better including the PadLeft solution entirely AND your alternative - you might well be the better answer then. This isn't about first, it's about best. – Jeff Yates Jan 28 '11 at 15:34
  • @Jeff: I prefer a separate post for each answer so that one can easily browse answers without having to wade through duplicates. However, I have made an exception today, and just for you have put 3 answers into a single post. – Gabe Jan 28 '11 at 16:44
  • The third answer was almost approximately what I was looking for. The pattern I needed was 00 00 00 00 ... i think i figured it out :) – THBBFT Oct 10 '14 at 01:41
1

Pad the string with zeros. In this case it is PadLeft(length, characterToPadWith). Very useful extension methods. PadRight() is another useful method.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32
  • 1
    `PadRight` is a useful method but not in this scenario. I recommend you remove that from your answer (or add every other useful .NET method - I know which I'd pick :D). – Jeff Yates Jan 28 '11 at 15:34
  • 1
    Also, they're not extension methods. – Gabe Jan 28 '11 at 15:54
0

You can create an extension method:

public static class ByteExtension
{
    public static string ToBitsString(this byte value)
    {
        return Convert.ToString(value, 2).PadLeft(8, '0');
    }
}
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162