Thorin Oakenshield Thorin Oakenshield -4 years ago 161
C# Question

Decimal to binary conversion in c #

How to convert a decimal no into binary in c #

Now I'm using like:

String str="8";
String Ans= Convert.ToInt32(str,2).ToString();

But it's throwing some Exceptions.

How to do this?

Answer Source

Your example has an integer expressed as a string. Let's say your integer was actually an integer, and you want to take the integer and convert it to a binary string.

int value = 8;
string binary = Convert.ToString(value, 2);

Which returns 1000.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download