I'm trying to convert a number from any base between 2-36 to any base between 2-36. I have the conversion to base 10, but I'm having trouble getting it from base 10 to the desired base. Any ideas? I know part of this idea will work, I just can't get it all together syntatically.
And don't worry about the checks (user inputs number 13, base 2), I'll add that at the end. Thanks!
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int ToTen ( char number[15], int base, int newbase );
void ToNewBase ();
int main()
{
char number[15];
int base = 0,
newbase = 0,
again = 0;
//do
//{
cout <<"Enter number: ";
cin >> number;
_strupr_s(number);
cout <<"Enter base of number (2-36): ";
cin >> base;
cout <<"Enter base you wish to convert to (2-36): ";
cin >> newbase;
ToTen ( number, base, newbase );
return 0;
}
int ToTen ( char number[15], int base, int newbase )
{
base;
newbase;
number[15];
char newnumber[100];
int answer = 0,
newanswer = 0,
i = 0,
k = 0,
digit = 0;
short placevalue = 0;
while ( number[i] != '\0' )
{
//for base 0-9
if ( number[i] <= '9' && number[i] >= '0' )
placevalue = number[i] - 48;
//for bases 10-36
else
placevalue = ( number[i] - 65 ) + 10;
answer = answer * base + placevalue;
i++;
}
cout<< "base ten: " << answer<< '\n';
while ( newnumber[k] != '\0' )
{
digit = answer % 10;
newnumber[k] = ( answer / 10 );
k++;
}
_strrev(newnumber);
cout << newbase << ": " << newnumber;
return 0;
}