'Design and develop a single page ASP.NET web application that converts a given number
I keep getting the following error: Cannot implicitly convert type 'int' to 'string'. I'm using C# for an ASP.web application
public string binTodec(int num)
{
int binaryNumber = int.Parse(Console.ReadLine());
int decimalValue = 0;
int base1 = 1;
while (binaryNumber > 0)
{
int reminder = binaryNumber % 10;
binaryNumber = binaryNumber / 10;
decimalValue += reminder * base1;
base1 = base1 * 2;
}
Console.Write($"Decimal Value : {decimalValue} ");
Console.ReadKey();
return decimalValue;
}
Any idea what my issue is??
Solution 1:[1]
The following is an algorithm that should work for your purposes:
public string binToDec(string bin)
{
int result = 0;
int exp = 0;
for (int i = bin.Length - 1; i > -1; i--, exp++)
{
double baseVal = 2 * double.Parse((bin.Substring(i, 1)));
if (baseVal == 0) continue;
double expVal = (double)(exp);
result += (int)(Math.Pow(baseVal, expVal));
}
return result.ToString();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | acornTime |