#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void HextoTwo(int num)
{
int res;
int i = 0;
char buf[BUFSIZ][5] = {"0000"};
char reference[16][5] = {"0000","0001","0010","0011",\
"0100","0101","0110","0111",\
"1000","1001","1010","1011",\
"1100","1101","1110","1111"};
while(num / 16 != 0)
{
res = num % 16;
strcpy(buf[i++], reference[res]);
num = num / 16;
}
res = num % 16;
strcpy(buf[i++], reference[res]);
while(i > 0)
printf("%s ", buf[--i]);
}
//采用递归
void HextoTwo(int num)
{
int remainder;
char buf[16][5] = {"0000","0001","0010","0011",\
"0100","0101","0110","0111",\
"1000","1001","1010","1011",\
"1100","1101","1110","1111"};
if(0 == num)
return;
remainder = num % 16;
HextoTwo(num >> 4);
printf("%s ", buf[remainder]);
}
int main()
{
int num = 0xfa;
HextoTwo(num);
return EXIT_SUCCESS;
}