在下面的代码中,有人可以让我了解代码如何用字母打印输出。代码的哪一部分将数字转换为字母?如果我输入999,它会将其转换为3E7。
class program
{
public static void Main()
{
int decimalNumber, quotient;
int i = 1, j, temp = 0;
char[] hexadecimalNumber = new char[100];
char temp1;
Console.WriteLine("Enter a Decimal Number :");
decimalNumber = int.Parse(Console.ReadLine());
quotient = decimalNumber;
while (quotient != 0)
{
temp = quotient % 16;
if (temp < 10)
temp = temp + 48;
else
temp = temp + 55;
temp1 = Convert.ToChar(temp);
hexadecimalNumber[i++] = temp1;
quotient = quotient / 16;
}
Console.Write("Equivalent HexaDecimal Number is ");
for (j = i - 1; j > 0; j--)
Console.Write(hexadecimalNumber[j]);
Console.Read();
}
}
答案 0 :(得分:4)
这里计算字符值:
if (temp < 10)
temp = temp + 48;
else
temp = temp + 55;
48是数字&#39; 0&#39;的ASCII代码,55是&#39; A&#39;的ASCII代码。
48 + 0 == '0'
48 + 1 == '1'
...
48 + 9 == '9'
55 + 10 + 0 == 'A'
55 + 10 + 1 == 'B'
...