输入十进制,输出十六进制
#include <stdio.h>
void dec2hex(char *hex,int num)
{
int i;
int j = 9;
unsigned int mask = 0x0000000f;
for(i = 0; i < 8; i++)
{
if(num & mask > 10)
{
hex[j] = (num & mask) - 10 + 'a';
}
else
{
hex[j] = (num & mask) + '0';
}
num = num >> 4;
j--;
}
}
int main(int argc, char **argv)
{
unsigned int num;
char hex[11];
hex[0] = '0';
hex[1] = 'x';
hex[10] = '\0';
printf("please input num:\n");
scanf("%u",&num);
dec2hex(hex,num);
printf("hex num:%s\n",hex);
return 0;
}