C语言之十进制转化为十六进制
代码如下所示:
#include <stdio.h>
int main() {
char hex[17] = { "0123456789ABCDEF" };
int c[20], i, n, base, dec;
printf("输入十进制正整数 ");
scanf_s("%d", &dec);
i = 0; base = 16; n = dec;
do {
c[i++] = dec % base;
dec = dec/base;
} while (dec);
printf("%d对应的十六进制数是:", n);
for (--i; i >= 0; i--)
printf("%c", hex[c[i]]);
printf("\n");
return 0;
}
输入
520
输出
208