IP address -- std::string to hex

topic posted Mon, February 27, 2006 - 4:52 PM by 
Share/Save/Bookmark
Advertisement

Hello, all ...

I'm wondering what the best / least # of lines way in C++ (so it's ok to use C++-isms like iostream, fstream, string, etc.) is to convert an IP address in dotted format (stored in a std::string) to an IP address in hex (stored in an array of chars)?

No, this is not a homework question -- i'm just in the middle of writing something like this (to search an ethereal dump file for RTP packets coming from a certain address) and want to know how _you_ would do it :-)

Regards,

John

Faling You - exploring the beauty of voice and sound
www.fallingyou.com
posted by:
Advertisement
Advertisement
  • Re: IP address -- std::string to hex

    Mon, February 27, 2006 - 7:19 PM
    char ipString[] = "222.100.1.32";
    char hexString[9];
    int a,b,c,d;

    sscanf(ipString, "%d.%d.%d.%d", &a, &b, &c, &d );
    sprintf( hexString, "%02X%02X%02X%02X", a, b, c, d );


    You can do it in two lines of ANSI C. Remove the "02" if you do not need the zero placeholder if the integer value is 15 or less. Use lower case letter "x" if you need the hex to contain lowercase hex letters (a through f).
    • Re: IP address -- std::string to hex

      Mon, February 27, 2006 - 8:05 PM
      nice and concise :)

      If the ip addess is not verified to be good however this code can overflow the buffer since sscanf will happily put much larger values than 255 into the int variables and sprintf will happily transfer the bytes into the target memory location without bounds checking.
  • Re: IP address -- std::string to hex

    Mon, February 27, 2006 - 8:09 PM
    1. Instead of writing a function, why not use a standard one like:
    inet_aton (const char *name, struct in_addr *addr)
    to convert it? Whatever C/C++ you're using must have one.
    2. Why store it in a char array? None of the standard functions will support it that way and you'll need to do a cast.

    You should stick to the standard functions not only for simplicity but for portability. They account for endian differences. Standard calls like ntohl() network-to-host-long, htonl() host-to-network-long convert values from the host machine's endian to network endian byte order.

    Doesn't Ethereal's built in filter handle what you need? I always found it best to capture everything and then use it''s filter on the output. Also, isolating as much traffic to begin with by putting it on a separate wire or hub really helps.
    • Re: IP address -- std::string to hex

      Mon, February 27, 2006 - 8:19 PM

      Hypno,

      I did it exactly this way -- by using the socket functions to do it (the C++ is gcc 4.0 on Mac OSX 10.4.5). I just wanted to know how others would do it. The sscanf() way I didn't think of, and aside from it being vulnerable to buffer overruns, it beats my code hands down in the least amount of lines (and perhaps the most efficient in terms of CPU cycles) dept.

      Yes, ethereal has capture filters, but I didn't read up on how to make one ... besides, I get to code in C++ precious little these days (I work in a mostly Perl shop, despite C++ being my favorite (and most experienced by _far_) language -- Perl is ok, but it's no C++). I recently got to code a small command-line tool that communicated over MIDI to a graphic equalizer (the platform was Win32, so I used the Win32 MIDI APIs to do it). It was fun, but it only took a couple of days, and then ... back to Perl.

      Regards,

      John

      Falling You - exploring the beauty of voice and sound
      www.fallingyou.com