Converting Binary to Decimal in C#
Below is a simple C# program to perform this conversion:
using System;
class Program
{
static int BinaryToDecimal(string binary)
{
int decimalValue = 0;
int baseValue = 1;
for (int i = binary.Length - 1; i >= 0; i--)
{
if (binary[i] == '1')
decimalValue += baseValue;
baseValue *= 2;
}
return decimalValue;
}
static void Main(string[] args)
{
string binaryNumber = "1001101";
int decimalNumber = BinaryToDecimal(binaryNumber);
Console.WriteLine($"Binary number {binaryNumber} is equivalent to decimal {decimalNumber}");
Console.ReadKey();
}
}
Output:
Binary number 1001101 is equivalent to decimal 77
Comments (0)