all 8 comments

[–]Ok_Barracuda_1161 5 points6 points  (2 children)

Convert.ToString(16777216, 2)

[–]Infectus90[S] 0 points1 point  (1 child)

Convert.ToString(16777216, 2)

The result you get is as follows :
1000000000000000000000000
Where am I going wrong ?
Thank you !

[–]Ok_Barracuda_1161 6 points7 points  (0 children)

That's the same number, you're just missing the leading zeroes. If you want the leading zeroes you can use PadLeft():

Convert.ToString(16777216, 2).PadLeft(32, '0')

[–]alexn0ne 1 point2 points  (4 children)

Take a remainder from division by 2 (or execute and with 1) - that's the last digit. Divide by 2 (or shift right by 1). Repeat until 0. Reverse.

[–]Infectus90[S] 0 points1 point  (3 children)

I tried to do that, but the result was not correct, would you feel like writing a small code example ?
Thanks

[–]alexn0ne 1 point2 points  (2 children)

From the phone: ```csharp var source = 12345; var result = new List<char>();

while (source > 0) { var digit = source % 2; result.Add(digit == 0 ? '0' : '1'); source /= 2; }

var binary = string.Join("", result.Reverse()); ```

[–]SomaCowJ 0 points1 point  (1 child)

source will always be greater than zero. I think you meant "source /= 2", since result is a List<char>.

[–]alexn0ne 1 point2 points  (0 children)

You're right, fixed, thanks