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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

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

Post 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? :)
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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.
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

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

Post 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.
Post Reply