Getting the byte bits as string
11,978
Solution 1
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;
}
Solution 2
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);
Solution 3
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();
}
Solution 4
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();
}
Author by
Hadad
Updated on June 18, 2022Comments
-
Hadad almost 2 years
I want to the bits (0 or one) from a byte in a string but I don't know how? Thanks.
-
Edward Ned Harvey almost 10 yearsThis 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, thenConvert.ToString(56,2).PadLeft(8,'0')
returns"00111000"
-
Edward Ned Harvey almost 10 yearsSee the other answer using Convert.ToString(value,2).PadLeft(8,'0') instead.
-
clweeks almost 10 yearsWas mine bugged? I converted it from VB and don't use C# regularly, so there's room for dumb error, but what is it?
-
Ivaylo Slavov almost 2 yearsThis 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.