08-27-04 11:21 PM
On Fri, 27 Aug 2004 00:17:31 +0000, Wim Deprez wrote:
> So the easiest way to do this, is something like:
>
> int main (int argc, char* argv[])
> {
> unsigned long ma = inet_addr("222.21.0.9");
>
> struct in_addr ia;
> ia.s_addr = ma;
>
> printf("%d - %s \n",ma,inet_ntoa(ia));
> return 0;
> }
The big thing to watch out for is that you don't overwrite the internal
buffer. Ie. this will not work...
printf("%d - %s\n%d - %s\n",
ma1, inet_ntoa(ia1),
ma2, inet_ntoa(ia2));
...which is why some people find it easier to do something like...
printf("%d - %u.%u.%u.%u\n%d - %u.%u.%u.%u\n",
ma1, IPv4(ia1),
ma2, IPv4(ia2));
...which always works fine, although looks a little ugly. You
can also have a custom inet_ntoa which uses a passed in buffer,
and just pass in multiple buffers.
Personally I solved it using custom formatters with my own printf like
function:
http://www.and.org/vstr/#cust-fmt
--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/
[ Post a follow-up to this message ]
|