Re: [RFC] convert a host address to a string

Mark Kettenis
2009-01-10T14:22:42Z

> Date: Sat, 10 Jan 2009 15:14:39 +0100
> From: "Kai Tietz" <ktietz70@googlemail.com>
>
> 2009/1/10 Mark Kettenis <mark.kettenis@xs4all.nl>:
> >> Date: Sat, 10 Jan 2009 14:30:29 +0100
> >> From: "Kai Tietz" <ktietz70@googlemail.com>
> >>
> >> ok, so I sugget the following patch instead. It is able to generate
> >> addresses for XP64 without the use of any vendor specific printf
> >> formatters, and uses for targets where sizeof(long) == sizeof(void*)
> >> the long variant.
> >
> > +#if defined(PRINTF_HAS_LONG_LONG) && BITSIZEOF_SIZE_T == 64 && \
> > + SIZEOF_LONG == 4
> > + sprintf (str, "0x%llx", (unsigned long long) (uintptr_t) addr);
> > +#elif BITSIZEOF_SIZE_T == 64 && SIZEOF_LONG == 4
> > + unsigned long long val = (unsigned long) (uintptr_t) addr;
> > + if ((val & ~0xffffffffull) != 0)
> > + sprintf (str, "0x%lx%08lx",
> > + (unsigned long) (val >> 32), (unsigned long) val);
> > + else
> > + sprintf (str, "0x%lx", (unsigned long) (uintptr_t) val);
> > +#else
> > + sprintf (str, "0x%lx", (unsigned long) (uintptr_t) addr);
> > +#endif
> > return str;
> > + BITSIZEOF_SIZE_T SIZEOF_LONG
> >
> > This is madness. If you go this route please do a simple
> >
> > const char *
> > host_address_to_string (const void *addr)
> > {
> > return phex_nz ((uintptr_t)addr, sizeof(addr));
> > }
> >
>
> hmm, well, but a "0x" has to be as prefix here, isn't it?

You're right. So something like:

{
char *result = get_cell ();
xsnprintf (result, CELLSIZE, "0x%s", phex_nz((uintptr_t)addr, sizeof(addr)));
return result;
}

perhaps?