123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
Program for Binary, Octal, Hexadecimal Conversions

--------------------------------------------------------------------------------



#include<stdio.h>
#include<conio.h>
#include<string.h>

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(" ****MAIN MENU****<BR>);
printf("Enter your choise.(1-12)<BR>);
printf("1. Binary to Decimal.<BR>);
printf("2. Decimal to Binary.<BR>);
printf("3. Decimal to Octal.<BR>);
printf("4. Decimal to Hexadecimal.<BR>);
printf("5. Octal to Decimal.<BR>);
printf("6. Octal to Binary.<BR>);
printf("7. Binary to Octal.<BR>);
printf("8. Binary to Hexadecimal.<BR>);
printf("9. Hexadecimal to Binary.<BR>);
printf("10. Hexadecimal to Decimal.<BR>);
printf("11. Octal to Hexadecimal.<BR>);
printf("12. Hexadecimal to Octal.<BR>);
scanf("%d",&n);
if(n<1 || n>12)
printf("Invalid Choise<BR>);
if(n==1)
bd();
else if(n==2)
db();
else if(n==3)
doc();
else if(n==4)
{
long a;
clrscr();
printf("Conversion from Decimal to Hexadecimal<BR>);
printf("Enter the decimal number.<BR>);
scanf("%ld",&a);
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("Conversion from Octal to Hexadecimal.<BR>);
printf("Enter a Octal number<BR>);
scanf("%ld",&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==0)
{
printf("Hexadecimal eq=");
oh(i);
}
else if(t==1)
printf("Numbert entered is not octal.<BR>);
}
else if(n==12)
ho();
printf("
Do you Wish to continue(Y/N)<BR>);
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("Conversion from Binary to Decimal<BR>);
printf("Enter Binary Number<BR>);
scanf("%d",&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("Entered number is not binary.");

}

void db()
{
int dec,bin,n,i=0,a[10];
clrscr();
printf("Conversion from Decimal to Binary<BR>);
printf("Input decimal no.");
scanf("%d",&dec);
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("Conversion from Decimal to Octal<BR>);
printf("Enter a Decimal number<BR>);
scanf("%d",&n);
i=0;
printf("Octal equavalent 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("%ld",i);
}
}

void od()
{
unsigned long n,i=0,a,p=1,t=0;
clrscr();
printf("Conversion from Octal to Decimal<BR>);
printf("Enter a Octal number<BR>);
scanf("%ld",&n);
i=0;
printf("Decimal equavalent of %ld",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(" can't be calculated because it is not an Octal Number.<BR>);
}

void ob()
{
int n,a[6],i=0,t=0;
clrscr();
printf("Convertion from Octal to Binary.<BR>);
printf("Enter an Octal Number.<BR>);
scanf("%d",&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<BR>);
}

void bo()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("Convertion From Binary to Octal<BR>);
printf("Enter a Binary number<BR>);
scanf("%ld",&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.
Printed value is not
correct.<BR>);
break;
}
}
if(t==1)
printf("Number is not Binary<BR>);
}

void bh()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("Convertion from Binary to Hexadecimal<BR>);
printf("Enter a Binary number<BR>);
scanf("%ld",&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.
Printed value is not
correct.<BR>);
break;
}
}
if(t==1)
printf("Number is not Binary<BR>);
}

void hb()
{
int i;
char s[20];
clrscr();
printf("Convertion from Hexadecimal to Binary<BR>);
printf("Enter a Hexadecimal number<BR>);
scanf("%s",s);
//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.<BR>);
break;
}
}
}

void hd()
{
int i,a[20];
unsigned long int h=0,m=1;
char s[20];
clrscr();
printf("Convertion from Hexadecimal to Decimal<BR>);
printf("Enter a Hexadecimal number<BR>);
scanf("%s",s);
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.
Printed value is not
correct.<BR>);
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("Convertion from Hexadecimal to Octal<BR>);
printf("Enter a Hexadecimal number<BR>);
scanf("%s",s);
// 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.
Printed value is not
correct.<BR>);
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 equavalent=");
while(h!=0)
{
a[i]=h%8;
h=h/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}

 

 

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved copyright 123ENG