[英]remove '\0' remove character from string array in c language and put instead replace it space ascii code
[英]C code showing ASCII instead of character
我过去只使用过 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');
}
}
您必须使用 '%c' 打印才能获得数字的 ASCII 表示,因为在 C 中,char 只是 unsigned short int
printf("%c ", hex[count-1]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.