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;
}