Hi everyone my Binary2Decimal is okay now because it can now hold 16 digits. But how come my Octal2Decimal and Hexa2Decimal can't hold 16 digits to convert to decimal it can only up to 5 digits. Can anyone correct this problem?
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char base;
char key;
void Binary2Decimal();
void Octal2Decimal();
void Hexa2Decimal();
void main()
{
do{
clrscr();
gotoxy(1,3);printf("Conversion from any base to base 10");
gotoxy(1,5);printf("a - Binary");
gotoxy(1,6);printf("b - Octal");
gotoxy(1,7);printf("c - Hexadecimal");
gotoxy(1,11);printf("Select a base to be converted: ");
scanf("%c", &base);
if((base=='a')||(base=='A')){
Binary2Decimal(); }
if((base=='b')||(base=='B')){
Octal2Decimal(); }
if((base=='c')||(base=='C')){
Hexa2Decimal(); }
if((base!='a')&&(base!='A')&&(base!='b')&&(base!='B')&&(base!='c')
&&(base!='C')){gotoxy(32,11);printf("Wrong letter"); }
gotoxy(1,20);printf("Press <ENTER> to Continue or Press <ANY KEY> to Exit");
scanf("%c", &key);
key = getch();
}
while(key == 13);
}
void Binary2Decimal()
{
char buffer[17];
int len,i,total=0,value;
gotoxy(1,13);printf("[BINARY TO DECIMAL CONVERSION]");
gotoxy(1,15);printf("Enter a Binary number: "); //accept a maximum of 16 chars
scanf("%16s", buffer);
printf("\n");
len=strlen(buffer);
for(i=0;i<len;++i)
{
value=buffer[i]-'0';
if (value!=0 && value!=1)
{
printf("Invalid binary data\n");
return;
}
total=(total<<1|value);
}
printf("Decimal equivalent is: %d\n", total);
}
void Octal2Decimal()
{
char oct8[17];
unsigned long dec8,i8,sum8,len8;
sum8=0;
gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");
gotoxy(1,15);printf("Enter an Octal number: ");
scanf("%16s", oct8);
printf("\n");
len8=strlen(oct8);
for(i8=0;i8<len8;++i8)
{
dec8=oct8[i8]-'0';
sum8=(sum8*8)+dec8;
}
printf("Decimal equivalent is: %u", sum8);
}
void Hexa2Decimal()
{
char hex16[17];
unsigned long dec16,i16,sum16,len16;
sum16=0;
dec16=0;
gotoxy(1,13);printf("[HEXADECIMAL TO DECIMAL CONVERSION]");
gotoxy(1,15);printf("Enter a Hexadecimal number: ");
scanf("%16s", hex16);
printf("\n");
len16=strlen(hex16);
for(i16=0;i16<len16;++i16)
{
if(isdigit(hex16[i16]))
{
dec16=hex16[i16]-'0';
}
else
{
dec16=toupper(hex16[i16])-'A'+10;
}
sum16=(sum16*16)+dec16;
}
printf("Decimal equivalent is: %u", sum16);
}