Float is already binary, period.
Everything is binary, but your result is "much less binary", because what you right into console is a string or a chain of characters, a human-readable representation of the binary. Let's assume this is what you really need, by some reason. It's still binary in that sense that your '1' or '0' characters are represented in memory as Unicode code points represented as 16-bit UTF-16LE words: 0x30 and 0x31.
The bits of any data type of the size no more then the size of of
System.Int32
can be found in this simplified way. Something like this:
static string ToBinaryString(float value) {
int bitCount = sizeof(float) * 8;
char[] result = new char[bitCount];
int intValue = System.BitConverter.ToInt32(BitConverter.GetBytes(value), 0);
for (int bit = 0; bit < bitCount; ++bit) {
int maskedValue = intValue & (1 << bit);
if (maskedValue > 0)
maskedValue = 1;
result[bitCount - bit - 1] = maskedValue.ToString()[0];
}
return new string(result);
}
If the case of arbitrary-size data, you need to serialize to bytes using
System.BitConverter
and work with individual bytes, in two nested loops.
See also:
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx[
^].
To see where are mantissa, exponent, etc., please see:
http://en.wikipedia.org/wiki/IEEE_floating_point[
^].
Learn the basics.
Good luck,
—SA