[SDL] convert IPaddress to string
1337p337
1337p337 at gmail.com
Mon Jul 17 16:40:31 PDT 2006
> > sprintf(ip_string, "%s", inet_ntoa((struct in_addr) ip.host);
> Is tha portable? I was thining in something like decomposing the int in
> four bytes and printing them in the string.
Yes, it's portable. That's actually what inet_ntoa does, but its buffer is
static so you'll want to strcpy it, or put it somewhere with sprintf. If you
look at glibc's version of inet_ntoa, you'll notice that splitting it into
individual bytes is exactly what they do:
bytes = (unsigned char *) ∈
__snprintf (buffer, 18, "%d.%d.%d.%d",
bytes[0], bytes[1], bytes[2], bytes[3]);
The way they do the split is kind of clever, since they don't do any shifting,
instead treating the int as a byte array. And it's portable across platforms
with different endianness, since IP addresses are stored in network byte order.
More information about the SDL
mailing list