Friday, 8 February 2013

binary to hexadecimal conversion in c++

#include<iostream.h>
#include<conio.h>

class conversion
{
private:
   int i,n,m,p,j,q,z,y,k;
   int bin[25],in[25],dec[25];
public:

void input()
{
cout<<"Add by yourself the zeros to the left side of the binary no";
cout<<"\n to bring it in the order of (4,8,12,----)\n\n";
cout<<"enter no of binary numbers you want to enter:        ";
cin>>n;
p=n/4;
for(i=0;i<n;i++)
{cout<<"enter binary number:    ";
cin>>bin[i];}

m=1;
for(i=3;i>=0;i--)
{in[i]=m;
m=m*2;}


}

void output()
{
cout<<"\nHexa conversion is:        ";
z=0;
y=4;
for(i=0;i<p;i++)
{
k=0;
for(j=z;j<y;j++)
{
dec[j]=bin[j]*in[k];
k++;}

q=0;
for(j=z;j<y;j++)
q=q+dec[j];

switch(q)
   {case 10:
   cout<<"A"<<"\t";
    break;
   case 11:
   cout<<"B"<<"\t";
    break;
   case 12:
   cout<<"C"<<"\t";
    break;
   case 13:
   cout<<"D"<<"\t";
    break;
   case 14:
   cout<<"E"<<"\t";
    break;
   case 15:
   cout<<"F"<<"\t";
    break;
    default:
    cout<<q<<"\t";
   }

cout<<"\t";
z=z+4;
y=y+4;
      }

}
};

void main()
{
conversion c;
c.input();
c.output();

getch();
}

0 comments:

Post a Comment