/*any suggestions on how to convert a hexadecimal no. into others and vice versa?d code might be long, i'm just 16, dnt be harsh :)*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<process.h>
main()
{
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<<"Enter choice-b,o,h,d";
cin>>ch;
len=strlen(a);
switch(a[0])//for converting the no. inputted into decimal
{
case 'O':
case 'o':base1=8;break;
case 'B':
case 'b':base1=2;break;
case 'H':
case 'h':base1=16;break;
}
switch(ch)//for final conversion
{
case 'O':
case 'o':base=8;break;
case 'B':
case 'b':base=2;break;
case 'H':
case 'h':base=16;break;
}
if(a[0]=='b'||a[0]=='B'||a[0]=='o'||a[0]=='O'||a[0]=='H'||a[0]=='h')
{
for(i=0;i<len;i++)
{
if(a[i]=='.')
pos=i;
}
for(i=pos-1,j=0;i>=1,j<pos-1;i--,j++)
{
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++)
{
a[i]=a[i]-48;
frac+=a[i]*(pow(base1,-j));
}
if(ch=='d'||ch=='D')
cout<<"Decimal no. is"<<frac+whole;
else
goto final;
}
else
{
for(i=0;i<len;i++)
{
if(a[i]=='.')
pos=i;
}
for(i=pos-1,j=0;i>=1,j<pos-1;i--,j++)//loop for storing whole part
{
a[i]=a[i]-48;
whole+=a[i]*(pow(10,j));
}
for(i=pos+1,j=1;i<len,j<=len-pos-1;i++,j++)//loop for storing fractional part
{
a[i]=a[i]-48;
frac+=a[i]*(pow(0.1,j));
}
goto final;
}
final:
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++;
}
if(ch=='d'||ch=='D')
{
getch();
exit(0);
}
cout<<"Coverted no.is ";
cout<<whole;
for(n=lenr-1;n>=0;n--)
cout<<r[n];
cout<<".";
for(h=0;h<lenq;h++)
cout<<q[h];
getch();
}
messr135 0 Newbie Poster
DeanMSands3 69 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.