Convert an integer to a binary string with leading zeros

binary, c#

Solution

`11` is binary representation of `3`. The binary representation of this value is `2` bits.

3 = 20 * 1 + 21 * 1

You can use `String.PadLeft(Int, Char)` method to add these zeros.

// convert number 3 to binary string. 
// And pad '0' to the left until string will be not less then 4 characters
Convert.ToString(3, 2).PadLeft(4, '0') // 0011
Convert.ToString(3, 2).PadLeft(8, '0') // 00000011

Problem

I need to convert int to bin and with extra bits. ``` string aaa = Convert.ToString(3, 2); ``` it returns `11`, but I need `0011`, or `00000011`. How is it done?

Original source