我过去只使用过 C++,我试图将我的十进制代码转换为十六进制代码到 C。尝试这样做时,我注意到当我尝试打印一个字符时,它会打印出 ASCII取而代之的价值。我不确定它为什么这样做。示例:十六进制中的 10 = A,但它会打印出 65。任何帮助将不胜感激。
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int num = 0;
printf("Please enter an integer ");
scanf("%d",&num);
//shown as 8 in the example
char hex[8];
char hex_values[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int count = 0;
while(num > 0)
{
int spot = num % 16;
hex[count] = hex_values[spot];
num = num / 16;
count++;
printf("%d ", hex[count-1]);
}
//places zeros in front of the array... in an easy way
int zeros = 8 - count;
for(int q = 0; q < zeros; q++)
{
printf("%c", '0');
}
}
答案 0 :(得分:0)
您必须使用 '%c' 打印才能获得数字的 ASCII 表示,因为在 C 中 char 只是 unsigned short int
printf("%c ", hex[count-1]);