8

I want to the bits (0 or one) from a byte in a string but I don't know how? Thanks.

5 Answers 5

37

Take a look at Convert.ToString(). You can use it both ways, for the conversion byte->bit-string and vice versa.

byte value = 56;
// There ...
string bits = Convert.ToString(value, 2);
// ...and Back Again
value = Convert.ToByte(bits, 2);
Sign up to request clarification or add additional context in comments.

1 Comment

This answer should be the accepted answer. When given only a single argument, Convert.ToString(byte) returns a hex string, but when given the second argument, Convert.ToString(byte,int) can use base 2, 8, 10, or 16. Convert.ToString(56,2) returns "111000" but if you want all the bits, then Convert.ToString(56,2).PadLeft(8,'0') returns "00111000"
4

Here's clweek's re-written method, that actually works:
I have used StringBuilder class instead of char array.

Sample: byteToBitsString(157) prints "00101111"

private string byteToBitsString(byte byteIn)
    {
        var bitsString = new StringBuilder(8);

        bitsString.Append(Convert.ToString((byteIn / 128) % 2));
        bitsString.Append(Convert.ToString((byteIn / 64) % 2));
        bitsString.Append(Convert.ToString((byteIn / 32) % 2));
        bitsString.Append(Convert.ToString((byteIn / 16) % 2));
        bitsString.Append(Convert.ToString((byteIn / 8) % 2));
        bitsString.Append(Convert.ToString((byteIn / 4) % 2));
        bitsString.Append(Convert.ToString((byteIn / 2) % 2));
        bitsString.Append(Convert.ToString((byteIn / 1) % 2));

        return bitsString.ToString();
    }

1 Comment

Was mine bugged? I converted it from VB and don't use C# regularly, so there's room for dumb error, but what is it?
2

Or the deluxe variant of the bit banging way to do it:

    public static string ToByteFormat(int valIn, int digits)
    {
        var bitsString = new StringBuilder(digits);
        int mask = (1 << digits - 1);
        for(int i = 0; i < digits; i++)
        {
            bitsString.Append((valIn & mask) != 0 ? "1" : "0");
            mask >>= 1;
        }
        return bitsString.ToString();
    }

Comments

0

Here's one bit banging way to do it:

public static string ByteToBinaryString(byte byteIn)
{
    StringBuilder out_string = new StringBuilder();
    byte mask = 128;
    for (int i = 7; i >=0 ; --i)
    {
        out_string.Append((byteIn & mask) != 0 ? "1" : "0");
        mask >>= 1;
    }
    return out_string.ToString();
}

Comments

-2

I bet there's a cleverer way to do this, but it works:

private string byteToBitsString(byte byteIn)
{
    char[] bits = new char[8];
    bits[0] = Convert.ToString((byteIn / 128) % 2);
    bits[1] = Convert.ToString((byteIn / 64) % 2);
    bits[2] = Convert.ToString((byteIn / 32) % 2);
    bits[3] = Convert.ToString((byteIn / 16) % 2);
    bits[4] = Convert.ToString((byteIn / 8) % 2);
    bits[5] = Convert.ToString((byteIn / 4) % 2);
    bits[6] = Convert.ToString((byteIn / 2) % 2);
    bits[7] = Convert.ToString((byteIn / 1) % 2);
    return bits;
}

2 Comments

See the other answer using Convert.ToString(value,2).PadLeft(8,'0') instead.
This answer is actually almost working, it had the wrong braces in the code, as it was converted from VB and the author probably did not have much experience in C# to spot the error. I fixed it. Obviously, this answer is quoted here and the quote gets positive score, it is IMHO reasonable to give a chance to the original answer to shine as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.