代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int dectohex(int b, char array[])
{
char a[17] = { "0123456789ABCDEF" };
int c[30] = { 0 }, i = 0, base = 16, j = 0;
while (b)
{
c[i++] = b % base;
b = b / base;
}
j = i;
for (i--; i >= 0; i--)
array[i] = a[c[i]];
return j;
}
int main()
{
char arr[100];
int b = 0, n = 0;
printf("请输入一个十进制数\n");
scanf("%d", &b);
n = dectohex(b, arr);
printf("%d的十六进制为:", b);
for (n--; n >= 0; n--)
printf("%c", arr[n]);
}