Help Converting Base to Base

'm trying to convert any number system up to base 16 to another base system. Both bases are given by the user as well as target number. Im trying to get my output to look like this-
Base number system-2
Target system-16
value in base-1110110011
The 1110110011 in base 2 is equal to 3B3 in base 16;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <math.h>
#include<iostream>
#include<string>
using namespace std;


int main()
{
string str;
string temp;
string digits("123456789ABCDEF");
int base, value, target;
int sum=0;

cout << "Enter the base : ";
cin >>base;
//Entering number system oct, hex or binary
cout<<"Enter the number : ";
cin>>str;
cout<<"Enter the target number system"<<endl;
cin>>target;
//Check for hex values
	for (int j=0; j = str.size()-1; j--)
		{
			if(std[j]=a||A)
				std[j]=10;
			else if(std[j]=b||B)
				std[j]=11;
			else if(std[j]=c||C)
				std[j]=12;
			else if(std[j]=d||D)
				std[j]=13;
			else if(std[j]=e||E)
				std[j]=14;
			else if(std[j]=f||F)
				std[j]=15;

		}
//getting to decimal for base conversion
	for (int i=0; i = str.size()-1; digit=0; i>0; i--; digit++)
		{
			temp=str[i];
			value=atoi(temp.c_str());
			sum+=value*pow((float)base, digit);
		}
//Base Conversion
int place=0;
number=sum;
while(number>0)
{
digit[place]=number mod target;
number=number div target;
place=place+1
}
//Final output
cout<<"The "<<std<< "in base "<<base<< "is equal to "<<number<<"in base " << target;
return 0;
}
Last edited on
Modified from earlier. Sorry for any confusion about what I was asking.
1. This stuff isn't doing what you think.
1
2
			if(std[j]=a||A)
				std[j]=10;


2. You shouldn't use floating point numbers, you need to stick with fixed point numbers.

3. mod is %. But as you want the divide and remainder, instead of doing the calculation twice, use div. http://en.cppreference.com/w/cpp/numeric/math/div
Last edited on
Topic archived. No new replies allowed.