Hi there. I'm trying to write a program that will convert any number from base x to base y (bases 2-16). The user will also be able to convert hexadecimal numbers like 4BFA from base 16 to base 2.. which is 100101111111010.
So far I've began to prompt the user for the number, ask for the old base, the new base. I am having trouble trying to convert the numbers that are input into a char array into integers so I can perform calculations on them. Essentially, I'd like to scan through each of the letters if it's a hex string and replace those with the equivalent numerical value.
Here is the code I have so far:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
char numIn[15];
int num[15];
char values[17] = "0123456789ABCDEF"; // array used for table lookup
int ob, nb, i;
cout << "Enter the the number you want to convert: ";
cin >> numIn;
// convert string to upper case *note* i'm using _strupr_s because vc '08 likes to give me a nasty deprication warning
_strupr_s(numIn);
cout << "\n Enter the original base: ";
cin >> ob;
// range checking
if(ob < 2 || ob > 16)
{
cout << "Bases are 2-16. Please re-enter:" << endl;
cin >> ob;
}
cout << "\n Enter the new base: ";
cin >> nb;
// range checking
if(nb < 2 || nb > 16)
{
cout << "Bases are 2-16. Please re-enter:" << endl;
cin >> nb;
}
// convert the char array to string
string s;
s = numIn;
cout << "\nAfter converting the char array, the string is: " << s << endl;
// find the string length for the loop
int length = strlen(numIn);
cout << "\nThe length of the string is: " << length << endl;
// loop through each subscript of the input
for(i=0; i < length; i++)
{
cout << num[i] << " ";
// check to see if it's a letter or number
if(isdigit(numIn[i]) == true)
{
num[i] = numIn[i] - 48;
}
else
{
num[i] = numIn[i] - 55;
}
}
// reverse the string for later use
cout << "\nAfter reversing the string, the string is: ";
for(i = length - 1; i >= 0; i--)
{
cout << numIn[i];
}
cout << endl;
return 0;
}
I know I still need my calculations, but I'd like to do this the right way before I start into that other part.
Any help would be greatly appreciated!