Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 二进制到十进制 int-rem,计数=0; 长整数n=0,b,i; 计数>b; i=b; 而(b>0) { 雷姆=b%10; n=n+rem*pow(2,计数); 计数++; b=b/10; } cout_C#_C++ - Fatal编程技术网

C# 二进制到十进制 int-rem,计数=0; 长整数n=0,b,i; 计数>b; i=b; 而(b>0) { 雷姆=b%10; n=n+rem*pow(2,计数); 计数++; b=b/10; } cout

C# 二进制到十进制 int-rem,计数=0; 长整数n=0,b,i; 计数>b; i=b; 而(b>0) { 雷姆=b%10; n=n+rem*pow(2,计数); 计数++; b=b/10; } cout,c#,c++,C#,C++,不,不是关键字,它是标准库的math.h中的函数 在这种情况下,您可以很容易地替换它,对于C++和C,都有位移位: 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,

不,
不是关键字,它是标准库的
math.h
中的函数

在这种情况下,您可以很容易地替换它,对于C++和C,都有位移位:

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();
(int)pow(2,count)==1否,
不是关键字,它是标准库的
math.h
中的函数

在这种情况下,您可以很容易地替换它,对于C++和C,都有位移位:

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();
(int)pow(2,count)==1请查看

一般来说,该类提供了许多您需要的功能

internet上其他地方的完整代码示例是。

请查看

一般来说,该类提供了许多您需要的功能

internet上其他地方的完整代码示例是。

请检查:

(int) pow(2, count) == 1 << count
选中此项:

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

您必须注意
C#

pow()
替换为
Math.pow()


您必须注意
C#

pow()
替换为
Math.pow()


我从来没有用C#编码过,但一个快速的谷歌说数学。Pow(2,count)是你想要的。非常感谢你的回答@taskinoor,但我想我无法摆脱由于类型转换而得到的结果,@javed帮了我。我从来没有用C#编码过,但一个快速的谷歌说数学。Pow(2,count)是你想要的。非常感谢你的回答@taskinoor,但我无法摆脱由于类型转换而产生的问题,@javed为此帮助了我。thanx@javed你真的解决了我的问题在((int)Math.Pow()的帮助下做起来很容易。正如@jaapjan告诉我的方法,但没有提到将其转换为int,我继续得到错误,谢谢……….thanx@javed你真的解决了我的问题在((int)Math.Pow()的帮助下很容易做到这一点。正如@jaapjan告诉我的方法,但没有提到将其复制为int的演员阵容,我继续得到错误,谢谢。。。。。。。。。。
 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;
    }