# Convert Hex to dec or Octal

The user is asked to input a number in either Hex, Octal or Decimal, which is then converted to Hex, Octal & Decimal.
The user can exit the program from the MAIN MENU or by the Escape key after each convertion.
Compiler : Borland Turbo C++ v4.5.

Ancient Dragon commented: Nice program, too bad you used such an old obsolete compiler. +24
``````//****************************************************************************//
//                                                                            //
// Program Name : ConvDec.cpp                                                 //
//                                                                            //
//   Programmer : K.Angel                                                     //
//              : Novice C++                                                  //
//                                                                            //
//  Description : The user is asked to input a number in either Hex, Octal or //
//                Decimal, which is then converted to Hex, Octal & Decimal.   //
//                The user can exit the program from the MAIN MENU or by      //
//                the Escape key after each convertion.                       //
//                                                                            //
//     Compiler : Borland Turbo C++ v4.5.                                     //
//         Date : 16/03/2006                                                  //
//                                                                            //
//****************************************************************************//

#include <iostream.h>
#include <conio.h>             // for clrscr() & getche() functions...
#include <stdlib.h>            // for exit() function...

const int ESC = 27;           // Used for escape sequince

int main()
{
int key = 0;
int choise = 0;
long number;

while(key != ESC)
{
cout << "Choose Your Number Base\n";
cout << "\n\t2. Octal.";
cout << "\n\t3. Decimal.";
cout << "\n\t4. Exit.\n\t       ";
cin  >> choise;

switch(choise)
{
case 1: clrscr();
cout << "Enter a Hex number: ";
cin  >> hex >> number;
cout << "\n   value in octal = "
<< oct << number << endl;
cout << " value in decimal = "
<< dec << number << endl;
break;
case 2: clrscr();
cout << "Enter a Octal number: ";
cin  >> oct >> number;
cout << "\n       value in hex = "
<< hex << number << endl;
cout << "   value in decimal = "
<< dec << number << endl;
break;
case 3: clrscr();
cout << "Enter a dec number: ";
cin  >> dec >> number;
cout << "\n   value in octal = "
<< oct << number << endl;
cout << "     value in hex = "
<< hex << number << endl;
break;
case 4:  clrscr();
cout << "Program terminated by user...";
exit(0);
break;                 //Unreachable code....
default : clrscr();
cout << "ERROR ~ Invalid selection\n\n";
break;
}
cout << "Press any key to continue or 'Esc' to exit";
key = getche();
clrscr();
}
cout << "Program terminated by user...";
return 0;
}``````

Move Clrscr() to between cin and switch and drop all the other clrscr() at case 1: thru case 4: and default:.
You are always Clrscr() anyway so why use the case at all for this?
This also saves some typing?

8000fe80 why this code is not working for this hexa decimal address

who the bullshit can write code like that.

Ancient Dragon 5,243

>>8000fe80 why this code is not working for this hexa decimal address
Data overflow.

On line 73 add cin.clear() to clear out the errors.

not working properly

here's d code i tried....it worked for all conversions

``````//final conversion-only for decimal input
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<process.h>
main()
{

//too long....the hex case ruins it all and too many switch cases

char a[30],ch;
int len,i,j,pos,whole=0,k=0,n=0,r[30],lenr=0,lenq=0,q[30],h=0,base,base1;
float frac=0;
cout<<"Enter number ";
gets(a);
cout<<"\nEnter choice-\n1. Decimal (d/D)\n2. Octal (o/O)\n3. Hexadecimal (h/H)\n4. Binary (b/B) ";
cin>>ch;
len=strlen(a);
switch(a[0])//for converting the no. inputted into frac and whole part
{
case 'O':
case 'o':base1=8;break;
case 'B':
case 'b':base1=2;break;
case 'H':
case 'h':base1=16;break;
case 'd':
case 'D':base1=10;break;
}
switch(ch)//for final conversion according to choice inputted
{
case 'O':
case 'o':base=8;break;
case 'B':
case 'b':base=2;break;
case 'H':
case 'h':base=16;break;
}
for(i=0;i<len;i++)//code for separating the fractional and whole part
{
if(a[i]=='.')
pos=i;
}
for(i=pos-1,j=0;i>=1,j<pos-1;i--,j++)
{
if(a[0]=='h'||a[0]=='H')
{
switch(a[i])
{
case 'A':whole+=10*(pow(base1,j));break;
case 'B':whole+=11*(pow(base1,j));break;
case 'C':whole+=12*(pow(base1,j));break;
case 'D':whole+=13*(pow(base1,j));break;
case 'E':whole+=14*(pow(base1,j));break;
case 'F':whole+=15*(pow(base1,j));break;
default:whole+=a[i]*(pow(base1,j));
}
}
else
{
a[i]=a[i]-48;
whole+=a[i]*(pow(base1,j));
}
}
for(i=pos+1,j=1;i<len,j<=len-pos-1;i++,j++)
{
if(a[0]=='h'||a[0]=='H')
{
switch(a[i])
{
case 'A':frac+=10*(pow(base1,-j));break;
case 'B':frac+=11*(pow(base1,-j));break;
case 'C':frac+=12*(pow(base1,-j));break;
case 'D':frac+=13*(pow(base1,-j));break;
case 'E':frac+=14*(pow(base1,-j));break;
case 'F':frac+=15*(pow(base1,-j));break;
default:frac+=a[i]*(pow(base1,-j));
}
}
else
{
a[i]=a[i]-48;
frac+=a[i]*(pow(base1,-j));
}
}
if(ch=='d'||ch=='D')
{
cout<<"\nConverted Decimal no. is "<<frac+whole;
getch();
exit(0);
}
else
{

while(whole>=base)//coverting whole part
{
r[n]=whole%base;
whole=whole/base;
n++;
lenr++;
}
while(h<4)//coverting fractional part
{
frac=frac*base;
q[h]=frac/1;
frac=frac-q[h];
h++;
lenq++;
}
cout<<"\nCoverted no.is ";
cout<<whole;
for(n=lenr-1;n>=0;n--)
{
if(ch=='h'||ch=='H')
{
switch(r[n])
{
case 10:cout<<"A";break;
case 11:cout<<"B";break;
case 12:cout<<"C";break;
case 13:cout<<"D";break;
case 14:cout<<"E";break;
case 15:cout<<"F";break;
default:cout<<r[n];
}
}
else
cout<<r[n];
}
cout<<".";
for(h=0;h<lenq;h++)
{
if(ch=='h'||ch=='H')
{
switch(r[n])
{
case 10:cout<<"A";break;
case 11:cout<<"B";break;
case 12:cout<<"C";break;
case 13:cout<<"D";break;
case 14:cout<<"E";break;
case 15:cout<<"F";break;
default:cout<<r[n];
}
}
else
cout<<r[n];
}
}
getch();
}``````

How can i convert hexadecimal to binary? Or kindly give me a c++ codes to convert it. Reply please. ASAP.

pritaeas 2,080

See code above.

If it's urgent for you, that doesn't mean it's urgent for us. Try to be polite.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.