I see what the problem is now; the lines
total=(total<<8)|8;
and
total=(total<<16)+8;
need to be
total=(total*8)+value;
and
total=(total*16)+value;
respectively. The reason the binary version worked with this approach is because it is working on the bits directly; the <<
operator is the left shift, which turns a bit pattern like 0000000000000101
into 0000000000001010
when shofting by 1. This works because it is the same effect as multiplying by the powers of 2. The 'bitwise or' operator, |
, has the effect of comparing each bit in pair of a bit patterns, and returning the result where if a bit is set in either pattern, it is set in the returned value. for example,
10011000
00101101
--------
10111101
In this case, since you have already shifted the value by one, then it has the effect of putting the single bit in value
into the least significant bit of the total. For example,
101
1
1 << 1 | 0 = 10 | 0 = 10
1 << 1 | 1 = 100 | 1 = 101
This works fine for binary conversion, but not for any higher numbers.
I see you're trying to get the indentation fixed, which is good, but the problem is you are mixing spaces and tabs; I personally use spaces, but the default in the Turbo C++ editor is tabs. This sometimes works, and sometimes doesn't. You also want to stay consistent in how you indent the code.
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char base;
char key;
void Binary2Decimal();
void Octal2Decimal();
void Hexa2Decimal();
int 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);
return 0;
}
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 buffer[7];
int len,i,total=0,value;
gotoxy(1,13);
printf("[OCTAL TO DECIMAL CONVERSION]");
gotoxy(1,15);
printf("Enter an Octal number: ");
scanf("%6s", buffer);
printf("\n");
len=strlen(buffer);
for(i=0; i<len; ++i)
{
value=buffer[i]-'0';
if(value< 0 && value > 7)
{
printf("Invalid octal data\n");
return;
}
total=(total<*8)+value;
printf("Decimal equivalent is: %d\n", total);
}
}
void Hexa2Decimal()
{
char buffer[5],digit;
int len,i,total=0,value;
gotoxy(1,13);
printf("[HEXADECIMAL TO DECIMAL CONVERSION]");
gotoxy(1,15);
printf("Enter a Hexadecimal number: ");
scanf("%4s", buffer);
printf("\n");
len=strlen(buffer);
for(i=0; i<len; ++i)
{
digit=toupper(buffer[i]);
if(digit>='0' && digit<='9')
{
value=digit - '0';
}
else if(digit>= 'A' && digit<= 'F')
{
value=digit - 'A'+10;
}
else
{
printf("Invalid hexadecimal data\n");
return;
}
total=(total*16)+value;
}
printf("Decimal equivalent is: %d\n", total);
}