base changer, char to digit

I am converting bases 2-16 to base 10 from a string. I dont know what to put in my for loop as well as the function to get the digit from the string. Any help?

1
2
3
4
5
6
7
8
9
10
11
12
13
  int convertToBase10 (string number, int base)
{
	int ans = 0;
	int x = base;

	for (        )
	{
		ans = ans * x + getDigit(?);
	}

	return ans;
}
Is my getDigit function looking somewhat correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int getDigit(string st,int & position)
{

	int numb = 0;
	unsigned int ii;

	for (ii = position; ii < st.length() ; ii++)
	{
		char c = st.at(ii);
		if (c >= '0'  && c <= '9')
		{
			numb = numb + (c - '0');

		}
		else
		{
			break;
		}

	}

	position = ii;
	return numb;
}
Last edited on
I'm going to use some letters to represent digits. Given a number "QWRTY" of base X, how do you compute its value in base 10?

Q..W..R..T..Y.
X4 X3 X2 X1 X0

The answer is Q * X4 + W * X3 + R * X2 + T * X + Y. (You'll need to convert Q, W, R, T, Y to their base 10 equivalents first.)

In case you don't know, these are the base 10 equivalents of the letters A to F:

A = 10
B = 11
C = 12
D = 13
E = 14
F = 15


Now just figure out how to do the calculation using a loop.
The calculation isnt the issue. Say i have 25 in base 4, that would be 13 in base 10. Instead my program is taking the sum of (2*4) and (5*4) getting 28, instead of (4*2) + 5. Cant figure out where i go wrong.
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
int convertToBase10 (string number, int base)
{
	int ans = 0;
	int pos = 0;
	int x = base;

	for (unsigned int k = 0; k < number.length(); k++)
	{
		ans = ans * x + getDigit(number, pos);
	}

	return ans;
}

int getDigit(string st,int & position)
{

	int numb = 0;
	unsigned int ii;

	for (ii = position; ii < st.length() ; ii++)
	{
		char c = st.at(ii);
		if (c >= '0'  && c <= '9')
		{
//			numb = numb * 10 + (c - '0');
			numb = numb + (c - '0');

		}
		else
		{
			break;
		}
	}

	position = ii;
	return numb;
}
Last edited on
There are at least two things wrong with your code.

1. Why is there a loop in getDigit()?
2. Work this out on paper for at least 3 iterations and see what happens (assume your getDigit is working correctly).
ans = ans * x + getDigit(number, pos);
On paper with at least 3 iterations gives a wrong answer (only off slightly). That was the equation my professor had told me to use. Would getDigit be the function that is working improperly?
What you're doing in getDigit() currently is using a loop to add all the digits of the string, from pos to the end, together. Is that really what you want? I see absolutely no reason for doing that.

If that's really want you want, let's walk through the loop for a couple iterations. Assume that number = "1111" and it's base 2. The decimal value for this is 15.

1st iteration:
    ans = 0 * 2 + getDigit("1111", 0); //getDigit() will return 4, and change pos to 4

2nd iteration:
    ans = 4 * 2 + getDigit("1111", 4); //getDigit() returns 0, and pos stays at 4

3rd iteration:
    ans = 8 * 2 + getDigit("1111", 4); //getDigit() returns 0, and pos stays at 4

4th (and final) iteration:
     ans = 16 * 2 + getDigit("1111", 4); //getDigit() returns 0, and pos stays at 4

^As you can see, we end up with "1111" in base 2 equal to 32 in base 10, which is wrong.

What I would use getDigit() for is to get the value of a single digit, like what the name implies. As in, I give it '1', and it returns 1. Or I give it 'A', and it returns 10.

Even if you change your getDigit() function to do that, the loop in your convertToBase10() is still wrong. It's because you're calling getDigit() with pos and not k. Assume that number = "1000" and it's base 2 (8 in decimal).

1st iteration:
    ans = 0 * 2 + getDigit("1000", 0); //getDigit() returns 1

2nd iteration:
    ans = 1 * 2 + getDigit("1000", 0); //getDigit() returns 1

3rd iteration:
    ans = 3 * 2 + getDigit("1000", 0); //getDigit() returns 1

4th (and final) iteration:
    ans = 7 * 2 + getDigit("1000", 0); //getDigit() returns 1

^In this case, ans == 15.
Last edited on
Topic archived. No new replies allowed.