Includes code to get each digit mathematically.
Code:
int DecToHex(int nDec) {
/* Converts a decimal number so that it looks the same when shown in
* hexadecimal. eg. 313 becomes 787 which is 0x313 in hexadecimal */
int nHex = 0, nHexBase = 1, nDecBase = 1, nDigitCount = 0, nNextDigitCount = 0;
do {
nDigitCount = nDec / nDecBase;
nNextDigitCount = nDec / (nDecBase * 10);
nDigitCount -= nNextDigitCount * 10;
nHex += nHexBase * nDigitCount;
nDecBase *= 10;
nHexBase *= 16;
} while (nNextDigitCount != 0);
return nHex;
}