Change of base function (i.e. hex to octal, decimal, etc.) - Output slightly off

I need to create a generic function that changes from any starting base, to any final base. I have everything down, except my original function took (and takes) an int value for the number that it converts to another base. I decided to just overload the function. I am Ok with changing between every base, but am slightly off when using my new function to take in a string hex value.

The code below should output 1235 for both functions. It does for the first one, but for the second, I am currently getting 1347. Decimal to Hex works fine - It's just the overloaded function (Hex to anything else) that is slightly off. I don't see the problem in my code yet, but I can assure you that I'm looking.

Thanks.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <stack>
#include <string>
#include <cmath>

using namespace std;

void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num);
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s);

int main()
{

    stack<int> myStack;
    string hexNum = "4D3";

    switchBasesFunction(myStack, 8, 10, 2323);

    cout << endl << endl;

    switchBasesFunction(myStack, 16, 10, hexNum);


    return 0;
}

void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num)
{

    int totalVal = 0;

    string s = to_string(num);

    for (int i = 0; i < s.length(); i++)
    {
        myStack.push(s.at(i) - '0');
    }

    int k = 0;

    while (myStack.size() > 0)
    {
        totalVal += (myStack.top() * pow(startBase, k++));
        myStack.pop();

    }

    string s1;

    while (totalVal > 0)
    {

        int temp = totalVal % finalBase;
        totalVal = totalVal / finalBase;

        char c;
        if (temp < 10)
        {
            c = temp + '0';
            s1 += c;
        }

        else
        {
            c = temp - 10 + 'A';
            s1 += c;
        }

    }
    for (int i = s1.length() - 1; i >= 0; i--)
    {
        cout << s1[i];
    }
    cout << endl << endl;
}

void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s)
{

    int totalVal = 0;

    for (int i = 0; i < s.length(); i++)
    {
        myStack.push(s.at(i) - '0');
    }

    int k = 0;

    while (myStack.size() > 0)
    {
        totalVal += (myStack.top() * pow(startBase, k++));
        myStack.pop();

    }

    string s1;

    while (totalVal > 0)
    {

        int temp = totalVal % finalBase;
        totalVal = totalVal / finalBase;

        char c;
        if (temp < 10)
        {
            c = temp + '0';
            s1 += c;
        }

        else
        {
            c = temp - 10 + 'A';
            s1 += c;
        }

    }
    for (int i = s1.length() - 1; i >= 0; i--)
    {
        cout << s1[i];
    }
    cout << endl << endl;
}
all you have to do is change the string to an int and then call your first function
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <stack>
#include <string>
#include <cmath>

using namespace std;

void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num);
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s);

int main()
{

	stack<int> myStack;
	string hexNum = "4D3";

	switchBasesFunction(myStack, 8, 10, 2323);

	cout << endl << endl;

	switchBasesFunction(myStack, 16, 8, hexNum);

	std::cin.ignore();
	return 0;
}

void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num)
{

	int totalVal = 0;

	string s = to_string(num);

	for (int i = 0; i < s.length(); i++)
	{
		myStack.push(s.at(i) - '0');
	}

	int k = 0;

	while (myStack.size() > 0)
	{
		totalVal += (myStack.top() * pow(startBase, k++));
		myStack.pop();

	}

	string s1;

	while (totalVal > 0)
	{

		int temp = totalVal % finalBase;
		totalVal = totalVal / finalBase;

		char c;
		if (temp < 10)
		{
			c = temp + '0';
			s1 += c;
		}

		else
		{
			c = temp - 10 + 'A';
			s1 += c;
		}

	}
	for (int i = s1.length() - 1; i >= 0; i--)
	{
		cout << s1[i];
	}
	cout << endl << endl;
}

void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s)
{
	int tempint = std::stoi(s, nullptr, startBase);
	switchBasesFunction(myStack, 10, finalBase, tempint);
}
Thanks for your help! It definitely works now.
Topic archived. No new replies allowed.