How can I output a number as a string of bits in C #?

In C #, how can I print a number as binary to the console? For example, if I have a uint with a value of 20, how can I type in the console: 00010100 (which is 20 in binary format)?

+3
source share
4 answers

Convert.ToString (myValue, 2);

+9
source

With the addition to make the value a byte:

Console.WriteLine(Convert.ToString(number, 2).PadLeft(8, '0'));
+6
source
int x = 3;
string binaryString = Convert.ToString(x, 2);
Console.WriteLine(binaryString);

11.

+3
Console.WriteLine(Convert.ToString(20, 2));
+2

Source: https://habr.com/ru/post/1702679/


All Articles