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!

Recommended Answers

All 4 Replies

fourstar,

I read your program. As you stated that some calculations are needed - my suggestion for you to write such a function that convert string (numeric values) into int and vice versa.

You can use this in your function :

* Test if string is a character and then decimal value of that character (assumed to be a hex value A-F or a-f) can be done by : dec_value = 10+(int)((int)string[i]-65) (for upper case) or dec_value = 10+(int)((int)string[i]-97) (for lower case) .

With the above procedure you'll get the decimal value of your hex digit now you can go on with your computations without any problem right ??? :)

Ok thanks for the help. I got that part figured out now. It turns out I was using

if(isdigit(numIn[i]) == true)

and I shouldn't have been using

== true

Now I'm trying to figure out how to reject any input that is from "G"-"Z" as those aren't valid hexadecimal letters. Any help?

Ok thanks for the help. I got that part figured out now. It turns out I was using

if(isdigit(numIn[i]) == true)

and I shouldn't have been using

== true

Now I'm trying to figure out how to reject any input that is from "G"-"Z" as those aren't valid hexadecimal letters. Any help?

Ascii value of 'G' is 71. Ascii value of 'Z' is 90. Any character between 71 and 90, inclusive, is to be rejected.

char digit = 'J';
if (digit >= 71 && digit <= 90)
     cout << "Illegal digit";

Have you considered using this function?
http://www.cplusplus.com/reference/clibrary/cctype/isxdigit/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.