/*
*Program for Binary, Octal, Hexadecimal Conversions
*
Created on March 25, 2009
*/
#include
#include
#include
#include
void bd();
void db();
void doc();
void dh();
void od();
void ob();
void bo();
void bh();
void hb();
void hd();
void oh();
void ho();
void main()
{
int n;
char c;
begin:
clrscr();
printf("\n\n @@ ## ~~ MAIN MENU ~~ ## @@\n\n ");
printf("\nPlease Enter y0ur ch0ise..!!(1-12) ");
printf("\n 1. Binary to Decimal. ");
printf("\n 2. Decimal to Binary. ");
printf("\n 3. Decimal to Octal. ");
printf("\n 4. Decimal to Hexadecimal. ");
printf("\n 5. Octal to Decimal. ");
printf("\n 6. Octal to Binary. ");
printf("\n 7. Binary to Octal. ");
printf("\n 8. Binary to Hexadecimal. ");
printf("\n 9. Hexadecimal to Binary. ");
printf("\n 10. Hexadecimal to Decimal. ");
printf("\n 11. Octal to Hexadecimal. ");
printf("\n 12. Hexadecimal to Octal. ");
printf("\n\n");
printf("Ch00se ConVersi0n fr0m...t0...(1-12):\n");
scanf("%d",& n);
if(n<1 || n>12)
printf("\n\n ");
printf("\n\nInvalid Choise ");
if(n==1)
bd();
else if(n==2)
db();
else if(n==3)
doc();
else if(n==4)
{
long a;
clrscr();
printf("\n\n");
printf("Conversion from Decimal to Hexadecimal ");
printf("\n");
printf(" Enter the decimal number...!!! ");
printf("\n\n\t");
scanf("%ld",&a);
printf("\n\n");
//dh(a);
}
else if(n==5)
od();
else if(n==6)
ob();
else if(n==7)
bo();
else if(n==8)
bh();
else if(n==9)
hb();
else if(n==10)
hd();
else if(n==11)
{
unsigned long n,i=0,a,p=1,t=0;
clrscr();
printf("\n\n");
printf("Conversion from Octal to Hexadecimal. ");
printf("\n");
printf(" Enter a Octal number...!!! ");
printf("\n\n\t");
scanf("%ld",&n);
printf("\n\n");
i=0;
while(n!=0)
{
a=n%10;
if(a>7)
t=1;
n=n/10;
i=i+a*p;
p=p*8;
}
if(t==1)
{
printf(" Hexadecimal eq= ");
//oh(i);
}
else if(t==0)
printf(" Numbert entered is not octal. ");
}
else if(n==12)
ho();
printf("\n\n\n");
printf(" Do you Wish to continue???..(Y/N) ");
printf("\n\t\t");
scanf("%s",&c);
c=toupper(c);
if(c=='Y')
goto begin;
getch();
}
void bd()
{
int n,b=0,a[6],i=0,t=0;
clrscr();
printf("\n\n");
printf("Conversion from Binary to Decimal. ");
printf("\n");
printf(" Enter Binary Number...!!! ");
printf("\n\n\t");
scanf("%d",&n);
printf("\n\n");
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]!=1 && a[i]!=0)
t=1;
i++;
}
a[i]=2;
n=1;
for(i=0;a[i]!=2;i++)
{
b=b+a[i]*n;
n=n*2;
}
if(t==0)
printf(" Decimal Equivalent=%d",b);
else if(t==1)
printf("\n\n");
printf("\nEntered number is not binary.");
}
void db()
{
int dec,bin,n,i=0,a[10];
clrscr();
printf("\n\n");
printf("Conversion from Decimal to Binary. ");
printf("\n");
printf(" Enter decimal number...!!!");
printf("\n\n\t");
scanf("%d",&dec);
printf("\n\n ");
do
{
a[i]=dec%2;
dec=dec/2;
i++;
}
while(dec!=0);
for(n=i-1;n>=0;n--)
printf("%d",a[n]);
}
void doc()
{
int n,i,a[10];
clrscr();
printf("\n\n");
printf("Conversion from Decimal to Octal. ");
printf("\n");
printf(" Enter a Decimal number...!!! ");
printf("\n\n\t");
scanf("%d",&n);
printf("\n\n");
i=0;
printf(" Octal equivalent of %d is ",n);
while(n!=0)
{
a[i]=n%8;
n=n/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}
void dh(long n)
{
long i;
if(n>0)
{
i=n%16;
n=n/16;
dh(n);
if(i>=10)
{
switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("\n\n");
printf("%ld",i);
}
}
void od()
{
unsigned long n,i=0,a,p=1,t=0;
clrscr();
printf("\n\n");
printf("Conversion from Octal to Decimal. ");
printf("\n");
printf(" Enter a Octal number...!!! ");
printf("\n\n\t");
scanf("%ld",&n);
printf("\n\n");
i=0;
printf(" Decimal equivalent of %ld is ",n);
while(n!=0)
{
a=n%10;
if(a>7)
t=1;
n=n/10;
i=i+a*p;
p=p*8;
}
if(t==0)
printf("=%ld",i);
else if(t==1)
printf("\n");
printf("\n Can't be calculated,,!!!\nBecause it is not an Octal Number. \n");
}
void ob()
{
int n,a[6],i=0,t=0;
clrscr();
printf("\n\n");
printf("Convertion from Octal to Binary. ");
printf("\n");
printf(" Enter an Octal Number...!!! ");
printf("\n\n\t");
scanf("%d",&n);
printf("\n\n");
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]>7)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("000");
break;
case 1:
printf("001");
break;
case 2:
printf("010");
break;
case 3:
printf("011");
break;
case 4:
printf("100");
break;
case 5:
printf("101");
break;
case 6:
printf("110");
break;
case 7:
printf("111");
break;
}
}
if(t==1)
printf(" Not a Octal number!!!\n");
}
void bo()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("\n\n");
printf("Convertion From Binary to Octal. ");
printf("\n");
printf("Enter a Binary number...!!! ");
printf("\n\n\t");
scanf("%ld",&n);
printf("\n\n");
while(n!=0)
{
a[i]=n%1000;
n=n/1000;
if(a[i]>111)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("0");
break;
case 1:
printf("1");
break;
case 10:
printf("2");
break;
case 11:
printf("3");
break;
case 100:
printf("4");
break;
case 101:
printf("5");
break;
case 110:
printf("6");
break;
case 111:
printf("7");
break;
default:
printf(" Entered number is not binary.\nPrinted value is not correct. ");
break;
}
}
if(t==1)
printf("Number is not Binary!!! ");
}
void bh()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("\n\n");
printf("Convertion from Binary to Hexadecimal. ");
printf("\n");
printf(" Enter a Binary number...!!! ");
printf("\n\n\t");
scanf("\t%ld",&n);
printf("\n\n");
while(n!=0)
{
a[i]=n%10000;
n=n/10000;
if(a[i]>1111)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("0");
break;
case 1:
printf("1");
break;
case 10:
printf("2");
break;
case 11:
printf("3");
break;
case 100:
printf("4");
break;
case 101:
printf("5");
break;
case 110:
printf("6");
break;
case 111:
printf("7");
break;
case 1000:
printf("8");
break;
case 1001:
printf("9");
break;
case 1010:
printf("A");
break;
case 1011:
printf("B");
break;
case 1100:
printf("C");
break;
case 1101:
printf("D");
break;
case 1110:
printf("E");
break;
case 1111:
printf("F");
break;
default:
printf(" Entered number is not binary.\n Printed value is not correct.\n");
break;
}
}
if(t==1)
printf("\nNumber is not Binary\n");
}
void hb()
{
int i;
char s[20];
clrscr();
printf("\n\n");
printf("Convertion from Hexadecimal to Binary.");
printf("\n");
printf(" Enter a Hexadecimal number...!!!");
printf("\n\n\t");
scanf("%s",s);
printf("\n\n");
//gets(s);
printf(" Binary Equivalent=");
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'a':
case 'A':
printf("1010");
break;
case 'b':
case 'B':
printf("1011");
break;
case 'c':
case 'C':
printf("1100");
break;
case 'd':
case 'D':
printf("1101");
break;
case 'e':
case 'E':
printf("1110");
break;
case 'f':
case 'F':
printf("1111");
break;
default:
printf(" Entered number is not Hexadecimal.\Printed value is not correct.\n");
break;
}
}
}
void hd()
{
int i,a[20];
unsigned long int h=0,m=1;
char s[20];
clrscr();
printf("\n\n");
printf("Convertion from Hexadecimal to Decimal.");
printf("\n");
printf(" Enter a Hexadecimal number...!!!");
printf("\n\n\t");
scanf("%s",s);
printf("\n\n");
printf(" Decimal Equivalent= ");
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
a[i]=0;
break;
case '1':
a[i]=1;
break;
case '2':
a[i]=2;
break;
case '3':
a[i]=3;
break;
case '4':
a[i]=4;
break;
case '5':
a[i]=5;
break;
case '6':
a[i]=6;
break;
case '7':
a[i]=7;
break;
case '8':
a[i]=8;
break;
case '9':
a[i]=9;
break;
case 'a':
case 'A':
a[i]=10;
break;
case 'b':
case 'B':
a[i]=11;
break;
case 'c':
case 'C':
a[i]=12;
break;
case 'd':
case 'D':
a[i]=13;
break;
case 'e':
case 'E':
a[i]=14;
break;
case 'f':
case 'F':
a[i]=15;
break;
default:
printf(" Entered number is not Hexadecimal.\nPrinted value is not correct.\n");
break;
}
}
i--;
for(;i>=0;i--)
{
h=h+a[i]*m;
m=m*16;
}
printf("%ld",h);
}
void oh(long n)
{
long i;
if(n>0)
{
i=n%16;
n=n/16;
oh(n);
if(i>=10)
{
switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("%ld",i);
}
}
void ho()
{
int i,a[20];
unsigned long int h=0,m=1;
char s[20];
clrscr();
printf("\n\n");
printf("Convertion from Hexadecimal to Octal.");
printf("\n");
printf(" Enter a Hexadecimal number...!!!");
printf("\n\n\t");
scanf("%s",s);
printf("\n\n");
// Converting hex to dec first
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
a[i]=0;
break;
case '1':
a[i]=1;
break;
case '2':
a[i]=2;
break;
case '3':
a[i]=3;
break;
case '4':
a[i]=4;
break;
case '5':
a[i]=5;
break;
case '6':
a[i]=6;
break;
case '7':
a[i]=7;
break;
case '8':
a[i]=8;
break;
case '9':
a[i]=9;
break;
case 'a':
case 'A':
a[i]=10;
break;
case 'b':
case 'B':
a[i]=11;
break;
case 'c':
case 'C':
a[i]=12;
break;
case 'd':
case 'D':
a[i]=13;
break;
case 'e':
case 'E':
a[i]=14;
break;
case 'f':
case 'F':
a[i]=15;
break;
default:
printf(" Entered number is not Hexadecimal.\nPrinted value is not correct.");
break;
}
}
i--;
for(;i>=0;i--)
{
h=h+a[i]*m;
m=m*16;
}
// Now convering from decimal to octal (h)
i=0;
printf(" Octal equivalent= ");
while(h!=0)
{
a[i]=h%8;
h=h/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}