convert a number from base 2 to base 10 in csharp

To convert a number from base 2 to base 10 in C#, you can use the Convert.ToInt32() method with 2 as the second parameter. This will convert the binary string to its decimal equivalent. Here's an example:

main.cs
string binary = "1101";
int decimalValue = Convert.ToInt32(binary, 2);
Console.WriteLine(decimalValue); // Output: 13
118 chars
4 lines

In the code above, we have a binary string "1101" which we want to convert to its decimal equivalent. We use the Convert.ToInt32() method and pass the binary string as the first parameter and 2 as the second parameter to specify that the input is in base 2. The method returns the decimal equivalent which is assigned to the decimalValue variable. Finally, we print the value of decimalValue to the console which will output 13.

gistlibby LogSnag