#include <stdio.h>
#include <conio.h>
#include <string.h>
long int [B]NumToDecimal[/B](char num[], int base)
{
long int d = 0;
int i, digit;
for (i = 0; num[i]; i++)
{
if ((num[i] >= '0') && (num[i] <= '9'))
digit = num[i] - '0';
else if ((num[i] >= 'a') && (num[i] <= 'f'))
digit = num[i] - 'a' + 10;
else if ((num[i] >= 'A') && (num[i] <= 'F'))
digit = num[i] - 'A' + 10;
else
digit = 16;
if (digit < base)
d = d * base + digit;
}
return d;
}
void [B]DecimalToNum[/B](long int d, char num[], int base)
{
int i = 0;
char digit;
do
{
digit = d % base;
d /= base;
if (digit <= 9)
digit += '0';
else
digit += 'A' - 10;
num[i] = digit;
i++;
} while (d > 0);
num[i] = 0;
strrev(num);
}
void main()
{
char num1[40], num2[40];
int base1, base2;
clrscr();
printf("Please enter a number : ");
gets(num1);
printf("Please enter base (2 to 16) : ");
scanf("%d", &base1);
printf("Please enter new base (2 to 16) : ");
scanf("%d", &base2);
[B]DecimalToNum[/B]([B]NumToDecimal[/B](num1, base1), num2, base2);
printf("number (base %d) = %s\n", base2, num2);
getch();
}