#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int btod(char *b)
{
int i;
int decimal = 0;
int place = 0, temp = 0;
for(i = strlen(b) - 1; i >= 0; i--)
{
if ((b[i] == '0') || (b[i] == '1'))
{
temp = b[i] - '0';
}
else
{
printf("error: not a valid binary number\n");
exit(0);
}
decimal += temp * pow(2, place);
place++;
}
return decimal;
}
int main(void)
{
char b[100];
printf("please input a binary number: ");
scanf("%s", b);
char h[100];
int q, r, i = 0, j;
q = btod(b);
printf("the decimal number for this binary number is: %d\n", q);
while(q != 0)
{
r = q % 16;
if ((r >= 0) && (r <= 9))
{
h[i++] = r + '0';
}
else
{
h[i++] = r - 10 + 'a';
}
q = q / 16;
}
printf("the hexagonal number for this binary number is: ");
for(j = i - 1; j >= 0; j--)
{
printf("%c", h[j]);
}
printf("\n");
return 0;
}