Trying to keep brain from getting rusty... coded some simple functions in C that takes a decimal and spits out binary and hex. Thought I might share it, enjoy!
#include
#include
void dec2bin(int dec);
void dec2hex(int dec);
int main(char *argv[])
{
int i,j,num,tmp;
scanf("%d",&num);
printf("dec: %d\n",num);
printf("bin: ");
dec2bin(num);
printf("\nhex: ");
dec2hex(num);
}
void dec2bin(int dec)
{
if (dec>0)
{
dec2bin(dec>>1);
if (dec&1>=1)
printf("1");
else
printf("0");
}
}
void dec2hex(int dec)
{
if (dec>0)
{
if (dec<10)
printf("%d",dec);
else if (dec>15)
{
dec2hex(dec/16);
dec2hex(dec%16);
}
else
printf("%c",dec-10+'A');
}
else
printf("0");
}
No comments:
Post a Comment