简体   繁体   English

二进制到十进制

[英]Binary to decimal

int rem, count = 0;
long int n=0, b, i;

count << "Enter the Binary value to convert in Decimal = ";
cin >> b;
i = b;

while (b > 0)
{
    rem = b % 10;
    n = n + rem * pow(2, count);
    count++;
    b = b / 10;
}

cout << "The decimal value of Binary no. = " << i << " = " << n;
getch();

I made this simple program in C++ and now I want to implement it in C# but I couldn't do so because I don't know how to implement the logic which I used in the loop. 我用C ++编写了这个简单的程序,现在我想用C#实现它,但是我不能这样做,因为我不知道如何实现循环中使用的逻辑。 Because in C++ the keyword pow is used to multiply the value of 2 so I don't know how to do it in C#. 因为在C ++中,关键字pow用于将2的值相乘,所以我不知道如何在C#中做到这一点。

No, pow() is not a keyword, it's a function from the standard library's math.h . 不, pow()不是关键字,它是标准库math.h的函数。

You can easily replace it in this case, for both C++ and C#, with bit shifting: 在这种情况下,对于C ++和C#,您可以轻松地通过移位将其替换:

(int) pow(2, count) == 1 << count

The above is true for all positive values of count , up to the limit of the platform's/language's precision. 以上对于count所有正值都是正确的,直到平台/语言的精度极限。

I believe the problem as a whole is much easier to solve using shifting. 我认为使用移位可以更轻松地解决整个问题。

Check this: 检查一下:

int bintodec(int decimal);

int _tmain(int argc, _TCHAR* argv[])
{ 
   int decimal;

   printf("Enter an integer (0's and 1's): ");
   scanf_s("%d", &decimal);

   printf("The decimal equivalent is %d.\n", bintodec(decimal));

   getchar();
   getchar();
   return 0;
}

int bintodec(int decimal)
{
   int total = 0;
   int power = 1;

   while(decimal > 0)
   {
      total += decimal % 10 * power;
      decimal = decimal / 10;
      power = power * 2;
   }

   return total;
}

Have a look at the Math.Pow Method . 看看Math.Pow方法

In general, the Math class provides much functionality you are looking for. 通常, Math类提供了您正在寻找的许多功能。

A complete code example elsewhere on the internet is enter link description here . 互联网上其他地方的完整代码示例是在此处输入链接描述

You have to take care of data types in C# 您必须照顾C#的数据类型

long int n=0, b, i;  // long int is not valid type in C#, Use only int type.

Replace pow() to Math.Pow() pow()替换为Math.Pow()

 pow(2, count);             // pow() is a function in C/C++
 ((int)Math.Pow(2, count))  // Math.Pow() is equivalent of pow in C#. 
                            // Math.Pow() returns a double value, so cast it to int
    public int BinaryToDecimal(string data)
    {
        int result = 0;
        char[] numbers = data.ToCharArray();

        try
        {
            if (!IsNumeric(data))
                error = "Invalid Value - This is not a numeric value";
            else
            {
                for (int counter = numbers.Length; counter > 0; counter--)
                {
                    if ((numbers[counter - 1].ToString() != "0") && (numbers[counter - 1].ToString() != "1"))
                        error = "Invalid Value - This is not a binary number";
                    else
                    {
                        int num = int.Parse(numbers[counter - 1].ToString());
                        int exp = numbers.Length - counter;
                        result += (Convert.ToInt16(Math.Pow(2, exp)) * num);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            error = ex.Message;
        }
        return result;
    }

http://zamirsblog.blogspot.com/2011/10/convert-binary-to-decimal-in-c.html http://zamirsblog.blogspot.com/2011/10/convert-binary-to-decimal-in-c.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM