I believe the
ToString
method will convert your
int
to an ASCII or Unicode representation, not a binary equivalent. You could cast your
int
to another appropriately sized type:
byte - 8-bits
ushort - 16-bits
uint - 32-bits
result = (byte)MyInt;
Finding the value of each bit is simple; use the & operator to AND your value with a constant
if (result & 0x01)
{ bool bit0 = true;}
if (result & 0x02)
{ bool bit1 = true;}
.
.
.
if (result & 0x80)
{ bool bit7 = true;}
It's a brute force method, and you'll end up with 8 different values, but that might be handy in some applications. Before you perform a cast from
int
to something smaller, be sure to check that the value is in the proper range for the type else you'll get an error. That is, if your
int
value is 256 or greater, don't try to cast it to a
byte
type.