You'd use it as:
void
puthexl (unsigned long value, FILE *f)
{
char buf[2 + CHAR_BIT * sizeof (value) / 4];
if (value == 0)
putc ('0', f);
else
{
char *p = buf + sizeof (buf);
do
*--p = "0123456789abcdef"[value % 16];
while ((value /= 16) != 0);
*--p = 'x';
*--p = '0';
fwrite (p, 1, buf + sizeof (buf) - p, f);
}
}
If the number is small, which is the common case,
this will iterate just small number of items
instead of always 16 times.
Anyway, generally, I wonder if replacing lots of
fprintf calls won't lead to less readable and maintainable
code, if many of the fprintfs will need to be replaced
e.g. by two separate calls (one fwrite, one puthexl
or similar).
Plus, what I said on IRC, regarding transformation
of fprintf calls to fwrite if there are no %s in
the format string, we should leave that to the host
compiler. It actually already does such transformations
for fprintf, but in this case we have fprintf_unlocked
due to system.h macros, and that isn't optimized by gcc
into fwrite_unlocked. That IMHO should be fixed on the
host gcc side though.