In C#, how can I output a number as binary to the console? For example, if I have a uint with the value of 20, how can I print to the console: 00010100 (which is 20 in binary)?
+2
A:
int x = 3;
string binaryString = Convert.ToString(x, 2);
Console.WriteLine(binaryString);
Console will display 11.
Brownie
2009-02-04 07:37:14
+3
A:
With padding to make the value a byte:
Console.WriteLine(Convert.ToString(number, 2).PadLeft(8, '0'));
Cameron MacFarland
2009-02-13 07:22:00