我目前在尝试让我的程序将二进制数转换为正确的十进制等值时遇到了很大的困难。有了我所拥有的,它给了我正确的数量,比如 111=7,但是当它应该是 13 时,它给了我错误的数量,比如 1101 = 11。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num;
//int n;
Console.WriteLine("Enter a number");
num = Convert.ToInt16(Console.ReadLine());
//n = num;
bintonum(num);
}
public static void bintonum (int num)
{
int dig;
double sum = 0;
while (num > 0)
{
dig = num % 10; //takes the number and breaks it down into each digit
sum = dig + (sum * 2); //reverses the number and adds the digit aquired from the previous line
num = num / 10; // reduces the number by one digit to get to zero
}
Console.WriteLine("{0}", sum);
}
}
}