2

i want to convert Decimal numbers to binary, Currently I'm using this way

private string strtoBin(string input)
{
    int number = Convert.ToInt32(input) ;
    string res = Convert.ToString(number, 2);

    return res;
}

it's working but when I'm having for example "6" I'm getting 110 instead of 0110? Any tips?!

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
WT86
  • 823
  • 5
  • 13
  • 34

1 Answers1

3

Simple string modification:

string res = Convert.ToString(number, 2);
res = new string('0', 8 - res.Length) + res;
Disposer
  • 6,201
  • 4
  • 31
  • 38