这是我以前写的10进制转换16进制的算法,供参考:
int XLTMath::dec2hex(int dec)
{
int sum = 0x0;
if (dec >= 0) { // 0 & positive
char str[20];
sprintf(str,"%d",dec);
int len = strlen(str);
int *buf = (int*)calloc(len,sizeof(int));
for (int i = 0; i < len; i++) {
buf = (dec >> i*4) & 0x0f;
sum += buf * (int)pow(16,i);
}
free(buf);
} else { // negative
const int SIZE = (INTSIZE == 2) ? 4 : 8;
int buf[SIZE];
int abs_dec = abs(dec);
for (int i = 0; i < SIZE; i++) {
buf = (((~abs_dec)+1) >> i*4) & 0x0f;
sum += buf*(int)pow(16,i);
}
}
return sum;
}