我的任务是编写一个程序,使用循环将十六进制整数转换为十进制的form.Do,而不是使用内置的.NET功能。
我已经写了程序,它适用于除"4ED528CBB4“之外的所有测试,并且它在"D”之后溢出。我使用long作为结果,但我找不到问题所在。
string hexadecimal = Console.ReadLine();
long result = 0;
for (int i = 0; i < hexadecimal.Length; i++)
{
if (hexadecimal[hexadecimal.Length - i - 1] >= '0' && hexadecimal[hexadecimal.Length - i - 1] <= '9')
{
result += ((hexadecimal[hexadecimal.Length - i - 1] - '0') * (int)Math.Pow(16, i));
}
else if (hexadecimal[hexadecimal.Length - i - 1] == 'D')
{
result += (13 * (int)Math.Pow(16, i));
}
else if (hexadecimal[hexadecimal.Length - i - 1] == 'C')
{
result += (12 * (int)Math.Pow(16, i));
}
else if (hexadecimal[hexadecimal.Length - i - 1] == 'A')
{
result += (10 * (int)Math.Pow(16, i));
}
else if (hexadecimal[hexadecimal.Length - i - 1] == 'B')
{
result += (11 * (int)Math.Pow(16, i));
}
else if (hexadecimal[hexadecimal.Length - i - 1] == 'F')
{
result += (15 * (int)Math.Pow(16, i));
}
else if (hexadecimal[hexadecimal.Length - i - 1] == 'E')
{
result += (14 * (int)Math.Pow(16, i));
}
}
Console.WriteLine(result);
}
}发布于 2016-11-04 05:48:22
如果你的reult参数是long,你不应该在其中进行类型转换吗?
https://stackoverflow.com/questions/40411567
复制相似问题