Trying to write a program that converts a Decimal number to a Hexadecimal number that implements recursion.
Can't use stuff like cout << hex << x << endl; Need to know how to write the function manually.
I tried this but it didn't really work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
string convertHex(int num, ostringstream &ss)
{
int x = num % 16;
switch (x)
{
case 10: ss << "A"; return ss.str(); break;
case 11: ss << "B"; return ss.str(); break;
case 12: ss << "C"; return ss.str(); break;
case 13: ss << "D"; return ss.str(); break;
case 14: ss << "E"; return ss.str(); break;
case 15: ss << "F"; return ss.str(); break;
default: ss << x; return ss.str(); break;
}
return convertHex(num / 16, ss);
}
Not sure if I fully understand how to convert from Decimal to Hex, but what's hanging me up more is getting it to work with recursion. Any ideas?
Well, for starters, your return after your switch statement will never execute.
Secondly, hexadecimal has a maximum of 8 bits, not 16, so your conversion should be modulating by 8.
Thirdly, as far as converting the whole number, the way you have this set up, you are only going to convert the lowest 8 bits. If you want to convert more, you will have to shift the bits as you convert them.
1 2 3 4 5 6 7 8
int x = num, y;
string converted_number;
for (int i=0; i<2; i++) {
y = x%8;
x = x << 8;
switch (x) {...non-returning-code-here...}
}
return converted_number;
Okay, so first off, thanks for pointing out that my recursive call never executed. Silly mistake :P
Second off, I posted this problem on stack overflow, and they suggested to add this if statement to the function:
string convertHex(int num, ostringstream &ss)
{
int x = num % 16;
if (num == 0)
{
ss << 0;
return ss.str();
}
switch (x)
{
case 10: ss << "A"; break;
case 11: ss << "B"; break;
case 12: ss << "C"; break;
case 13: ss << "D"; break;
case 14: ss << "E"; break;
case 15: ss << "F"; break;
default: ss << x; break;
}
return convertHex(num / 16, ss);
}
and now it's actually working.... but not really.
When I enter in a number, it will give me the right hex value... But it'll be backwards.
So, if I enter in 16 to convert to Hex, it'll give me 010. I figured it was correct, and added a zero on the end, but when I gave it a bigger number, 4598, it gave me 6F110. The correct Hex value for 4598 is 11F6.
Let me first apologize, my first response was late at night, and I muddled the math. You were correct in using modulus 16. And the shift should be 4 bits not 8.
The reason it is backwards, is because the way this is written, you are writing the least significant digit first, and the highest significant digit last. Probably the easiest way to fix this is ...
Restructure such that the if statement is first. Following that, the recursive function call. Lastly, the actual math. This will cause the most significant digits to be written first, and the least significant, last.
string convertHex(int num, ostringstream &ss)
{
if (num == 0)
{
ss << 0;
return ss.str();
}
convertHex(num / 16, ss);
int x = num % 16;
switch (x)
{
case 10: ss << "A"; break;
case 11: ss << "B"; break;
case 12: ss << "C"; break;
case 13: ss << "D"; break;
case 14: ss << "E"; break;
case 15: ss << "F"; break;
default: ss << x; break;
}
return ss.str();
}