Decimal to Hexadecimal

I'm not too sure how well I did when I wrote this function. It does work just fine, but I'm worried that there might be a better way of doing this (in fact, I'm fairly sure there is).

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
string toHexadecimal(int num)
{
  int dividend, remain;
  string result = "";
  string hexArray[16];
  hexArray[0] = "0";
  hexArray[1] = "1";
  hexArray[2] = "2";
  hexArray[3] = "3";
  hexArray[4] = "4";
  hexArray[5] = "5";
  hexArray[6] = "6";
  hexArray[7] = "7";
  hexArray[8] = "8";
  hexArray[9] = "9";
  hexArray[10] = "a";
  hexArray[11] = "b";
  hexArray[12] = "c";
  hexArray[13] = "d";
  hexArray[14] = "e";
  hexArray[15] = "f";
  dividend = (int)num/16;
  remain = num%16;
  result = hexArray[remain];
  while (dividend != 0)
  {
    remain = dividend%16;
    dividend = (int)dividend/16;
    result = hexArray[remain]+result;
  }
  return result;
}

I need the function to return a string with the result. I know that C++ has a method of doing it itself, but it seems to only display the result and not save it in a variable like I need it to. Any suggestions?
You can use stringstreams
eg
1
2
3
stringstream ss;
ss << hex << your_number;
string result = ss.str();

http://www.cplusplus.com/articles/numb_to_text/#cf
Thank you. I've been meaning to study other forms of streams besides files. Guess it's a good a time as any now.
Topic archived. No new replies allowed.