Author : MD TAREQ HASSAN | Updated : 2021/03/22
int to binary
// int to binary
int foo = 5;
string binary = Convert.ToString(foo, 2);
Console.WriteLine(binary); // 101
binary to int
// binary to int
var bar = Convert.ToInt32("101", 2);
Console.WriteLine(bar); // 5
Convert from any classic base to any base
// Convert from any classic base to any base in C#
// Supported bases are 2, 8, 10 and 16
String number = "100";
int fromBase = 16;
int toBase = 10;
String result = Convert.ToString(Convert.ToInt32(number, fromBase), toBase);
Binary with padding left
string bin = Convert.ToString(1234, 2).PadLeft(16, '0');
Console.WriteLine(bin); // 0000010011010010