我打算做一个这样的程序:
loop
read first character
read second character
make a two-digit hexadecimal number from the two characters
convert the hexadecimal number into decimal
display the ascii character corresponding to that number.
end loop
我遇到的问题是将这两个字符转换为十六进制数,然后再将其转换为十进制数。一旦我有了十进制数,我就可以显示 ascii 字符。
最佳答案
除非你真的想自己编写转换,否则你可以使用 [f]scanf 使用 %x
转换读取十六进制数,或者你可以读取一个字符串,然后使用 (for one可能性)strtol
。
如果您确实想自己进行转换,可以像这样转换单个数字:
if (ixdigit(ch))
if (isdigit(ch))
value = (16 * value) + (ch - '0');
else
value = (16 * value) + (tolower(ch) - 'a' + 10);
else
fprintf(stderr, "%c is not a valid hex digit", ch);
关于c - 如何在 C 中将十六进制数转换为 Ascii 码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5087823/