Page 1 of 1

C++: Convert Uint32-IP to std::string

Posted: Thu Aug 27, 2009 4:20 pm
by dani93
Now I'm gonna show you a piece of code I often use with SDLNet.
It converts a Uint32-IP-address (like used by SDLNet) to a std::string. I used it in a messenger to display "* nick [192.168.0.x] connected" in the console and GUI (TTF).
It's pretty easy if you know how an IP is built up and you can calculate a bit in hex. For those who can't do that, I'll explain it shortly :)
An IP consists of 4 * 1 Byte with '.' inbetween (e.g. 127.0.0.1). A hex number takes 4 bits (a half byte). So you just isolate the last two hexadecimal digits of the Uint32 and you get a byte of the address. Then you convert it to ASCII, reverse it (wrong direction due to string adding!) and add it to a string. You do that until there are no digits left in the Uint32. The string is finally returned.

Code: Select all

string ConvertIP (Uint32 hexip)
{

  Uint32 byte, intbuffer, digit;
  string buffer, stringip;
  string::reverse_iterator i;

  while (hexip > 0)               // run until there are no digits left
  {

    byte = hexip % 0x100;         // get last two digits (1 Byte)

    hexip /= 0x100;               // cut the last two digits off

    intbuffer = byte;             // make a copy of the byte

    buffer.clear ();              // clear the string

    do                            // now convert it to ASCII
    {

      digit = intbuffer % 10;     // store last digit

      intbuffer /= 10;            // cut off last digit

      buffer += digit + 48;       // add '0' to get ASCII-values

    } while (intbuffer > 0);      // run until there are no digits left in the byte

    for (i = buffer.rbegin (); i != buffer.rend (); ++i)   // reverse the string
      stringip += *i;

    if (hexip > 0)                // add a '.' if it is not the end of the IP
      stringip += '.';

  }

  return stringip;                // return the string

}
Useful for anyone? Or any other comments on it? :)

Re: C++: Convert Uint32-IP to std::string

Posted: Fri Aug 28, 2009 12:21 am
by M_D_K
Yeah inet_ntoa serves the same function as that does. inet_ntoa is used in BSD sockets and WinSock(which SDL_net wraps).

Code: Select all

char *inet_ntoa(struct in_addr in)
{
        static char b[18];
        char *p;

        p = (char *)∈
#define UC(b)   (((int)b)&0xff)
        (void)snprintf(b, sizeof(b), "%u.%u.%u.%u", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
        return (b);
}
And although in_addr is a struct it's being treated as a u32 int since that's the only thing it holds(well technically it's being treated as a char array). The reason it's being treated as a char array is so it can grab each part of the IP one byte at a time. See 32bit ints are 4 bytes.

the UC define just does a bitwise and on each part. 0xff cause in IPv4 255 is the max and 0xff is 255.
Take 127.0.0.1 for example in hex it's 0x0100007f. so what lets break it down.
0x7f & 0xff = 127
0x00 & 0xff = 0
0x00 & 0xff = 0
0x01 & 0xff = 1

So yeah, cleaner/easier way of doing it.

Re: C++: Convert Uint32-IP to std::string

Posted: Fri Aug 28, 2009 9:17 am
by dani93
Damn, just reinvented the wheel...
I already worked a bit with BSD-sockets, but I didn't know WinSock supports this too.
Thanks for that hint.