954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Number System Conversions

/*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
#include
#include
#include
#include
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=1,j=1,j=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<=0;n--)
cout<

messr135
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 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.

DeanMSands3
Junior Poster
187 posts since Jan 2012
Reputation Points: 37
Solved Threads: 27
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You