Можно ли заоптимизировать эту функцию?
char * intToHex(int32_t value, uint32_t up, uint32_t pre, uint32_t msize)
{
const char * hexdigit[2] = {"0123456789abcdef",
"0123456789ABCDEF"};
union itou
{
int32_t i;
uint32_t u;
};
union itou conv;
conv.i = value;
uint32_t pos = 0;
char * result;
/* 0x FF FF FF FF \0 */
if (!(result = malloc(sign(pre) * 2 + sizeof(uint32_t) * 2 + 1)))
return 0;
if (pre)
{
strcpy(result, "0x");
pos = 2;
}
while (1UL << (msize * 4) > conv.u)
{
result[pos++] = '0';
msize--;
}
while (conv.u != 0)
{
result[pos++] = hexdigit[sign(up)][conv.u & 0xF];
conv.u >>= 4;
}
result[pos] = '\0';
return result;
}