Friday, 11 January 2013

Convert a number from one base to another


#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
#include<math.h>
#include<string.h>
class base
{
  private:
          int oldbase,newbase,len,i,j,d,val;
          long dec;
          char oldnum[50],newnum[100];
  public:
          void getdata();
          void getdisp();
};
int value(char dig)
{
           switch (toupper(dig))
          {
                   case'0' : return(0);
                   case'1' : return(1);
                   case'2' : return(2);
                   case'3' : return(3);
                    case'4' : return(4);
                   case'5' : return(5);
                   case'6' : return(6);
                   case'7' : return(7);
                   case'8' : return(8);
                   case'9' : return(9);
                   case'A' : return(10);
                   case'B' : return(11);
                   case'C' : return(12);
                   case'D' : return(13);
                     case'E' : return(14);

                   case'F' : return(15);
                   default:return(-1);
          }
}
char letter(int dig)
{        switch (dig)
          {        case 0: return '0';
                   case 1: return '1';
                   case 2: return '2';
                   case 3: return '3';
                   case 4: return '4';
                   case 5: return '5';
                   case 6: return '6';
                   case 7: return '7';
                   case 8: return '8';
                   case 9: return '9';
                   case 10: return 'A';
                   case 11: return 'B';
                   case 12: return 'C';
                   case 13: return 'D';
                   case 14: return 'E';
                   case 15: return 'F';
                //  default:return'-1';
          }
}
void base::getdata()
{
          cout<<"\n\tBASE CONVERSION\n\n\n";
          cout<<"What is the base of the number to be converted : ";
          cin>>oldbase;
          cout<<"Enter your number : ";
          cin>>oldnum;
          cout<<"What is the new base? : ";
          cin>>newbase;
}
void base::getdisp()
{
          len=strlen(oldnum);
          for(i=len-1,j=0;i>=0;--i,j++)
          {        val=value(oldnum[i]);
                   if((val>oldbase-1)||(val==-1))
                   {        cout<<"Invalid charecter ";
                             getch();
                             exit(1);
                   }
                   dec=dec+pow(oldbase,j)*val;
          }
          for(i=0;dec>0;i++)
          {        d=dec%newbase;
                   newnum[i]=letter(d);
                   dec=dec/newbase;
          }
          newnum[i]='\0';
cout<<"\nNumber "<<oldnum<<" in base "<< oldbase<<" is                     "<<strrev(newnum)<<" in base "<<newbase;
}
void main()
{
          base num;
          clrscr();
          num.getdata();
          num.getdisp();
          getch();
}

No comments:

Post a Comment