I'm working on an embedded system, where the printf only supports %x,%s and %c. To keep my logs human-readable, I'd like many of the numbers to be printed in decimal. Here's one simple, elegant, but inefficient (and with limitations) version. I use a function to convert the decimal to a (bigger) hex number that would print right when used with %x. The generated number is otherwise useless. This is small enough for the footprint to be tolerable, even in my embedded system:
int dize(int i) {
int r = 0, p = 1;
while (i > 0) {
r += i % 10 * p;
i /= 10;
p *= 16;
}
return r;
}
And a sample usage:
main () {
printf("%x\n", 2010);
printf("%x\n", dize(2010));
}
Would print: