Decimal to Hex Conversion - Not getting correct output

Hi all,

Just trying to do a standard dec to hex conversion. I'm getting the output 6C6CC when the answer should be 6C0. I'd really appreciate the help!

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void decimalToHex(int number, string s);
char retrieveHexChar(int num);

int main(){
    
    int num = 1728;
    string s1;
    
    decimalToHex(num, s1);
    
}

void decimalToHex(int number, string s){
      
  stringstream ss;
      
  if(number > 0){

char tempChar;
int temp = number % 16;
number = number/16;


if (temp >= 10){

tempChar = retrieveHexChar(temp);
s += tempChar;

}

else if (temp < 10){
    
    ss << temp;
    string str = ss.str();
    s += str;  
    
}

decimalToHex(number, s);

}

for (int i = s.length(); i > 0; i--)
    cout << s[i];
}

char retrieveHexChar(int num){
    
    char tempChar;
    
    switch(num){
    
    case 10:
    tempChar = 'A';
    break;
    
    case 11:
    tempChar = 'B';
    break;
    
    case 12:
    tempChar = 'C';
    break;
    
    case 13:
    tempChar = 'D';
    break;
    
    case 14:
    tempChar = 'E';
    break;
    
    case 15:
    tempChar = 'F';
    break;    
}
    return tempChar;
    
}
Add this before line 23:
cout << "dtoh(" << number << ',' << s <<")\n";
and this before line 50:
cout << '\n';

The output is now:
dtoh(1728,)
dtoh(108,0)
dtoh(6,0C)
dtoh(0,0C6)
6C
6C
C

So the problem is that each recursive call prints a part of the output.

This will sound harsh, but I think you should throw this out and start again. There is no need for recursion, or retrieveHexChar, or a stringstream. The function should return a string that main() outputs.

So the function should be
string numberToHex(int number)

You have the right idea for stripping off one hex digit at a time:
1
2
int temp = number % 16;
number = number/16;

Next you need to get the hex character that corresponds to temp. Here's one way to do it:
1
2
3
char ch;
if (temp <10) ch = temp + '0';
else ch = temp-10 + 'A';

This works when the numeric code for digits are consecutive from 0 to 9, and then the code for letters is consecutive from A to (at least) F. The people who developed ASCII were smart enough to do this.

Another way to do it is with a lookup table:
1
2
const char hexDigits = "0123456789ABCDEF";
char ch = hexDigits[temp];


Once you have the digit, append it to the result string.

Now put the whole thing inside a loop instead of loop:
1
2
3
4
while (number) {
     int temp = number % 16;
     etc.
}


This will produce a string that is in the reverse order of what you want. Use string::reverse() to reverse it.
Thanks! Your answer is not harsh at all! My aim is to do things the right way.
Last edited on
One last quick question...If I change this to a void function, and then add the following at the end to reverse the string:

1
2
for (int i = s.length() - 1; i >= 0; i--)
cout << s.at(i);


I get the following: 0C06C0

If I do the same loop in main (after assigning a new string the returned string from the function), I get the correct answer. Kind of consused about that. I'll try to figure it out, though.

EDIT: got it now - had to do with using s.at(i), instead of just s[i].
Last edited on
Topic archived. No new replies allowed.