0
/*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();
}

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by DeanMSands3
0

Well, for starters, you want to put your code inside the [ CODE ] blocks. It preserves the formatting.
Looking at your code isn't as much fun without indentation.

And... and... you have used GOTO. And you have sinned.

Just kidding. (But you may want to look into ways around that.)

I would break the code down into functions to make it more modular and also easier (MUCH EASIER) to read.

Before we go any farther, I will give you an out. Everything you want to do CAN be done using cout and cin and the dec, oct, & hex modifiers. And also scanf with
the x modifier will work.

Now, to get you started, I want you to look at this:

a[i]=a[i]-48;

That works for numbers. And only numbers. If you want to add hexadecimal, you need to look beyond numbers.
Try this.

if(a[i]>='0'&&a[i]<='9'){	//Only use this conversion for numbers
			   a[i]=a[i]-48;
		   }else if(a[i]>='A'&&a[i]<='F'){	//Only use for upper case letters
			   a[i]=a[i]-64;
		   }else if(a[i]>='a'&&a[i]<='f'){	//Only use for upper case letters
			   a[i]=a[i]-96;
		   }

More to follow.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.