What is the easiest way to get a binary representation of an integer?

Actually, I am not asking how to implement this functionality myself. I know that it will not be very difficult. I just don't want to reinvent the wheel, so I was wondering if this functionality exists somewhere in the BCL. It seems to be out there somewhere ...

Example of input / desired output:

Input output
eleven
2 10
3 11
4,100
10 1010
+3
source share
2 answers

How about System.Convert.ToString(int value, int toBase)with toBase set to 2?

+11
source

Try Convert.ToString, for example:

Console.WriteLine(Convert.ToString(1, 2));
Console.WriteLine(Convert.ToString(2, 2));
Console.WriteLine(Convert.ToString(3, 2));
Console.WriteLine(Convert.ToString(4, 2));
Console.WriteLine(Convert.ToString(10, 2));

- , ( 2).

+7

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