String Manipulation / Juxtaposition

This is a discussion on String Manipulation / Juxtaposition within the C Programming forums, part of the General Programming Boards category; Would someone be kind enough to help me with this seemingly simple (yet difficult for a non-C guy like me) ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    String Manipulation / Juxtaposition

    Would someone be kind enough to help me with this seemingly simple (yet difficult for a non-C guy like me) problem...

    Assuming buf contains a string similar to either one of these:

    Code:
    50005112601012121148\n in buf
    ... or ...
    600000112401092112156\n in buf
    How do I juxtapose/manipulate the numbers so that I end up with the following:

    Code:
    010.112.148.212 in ip, and 5000 in port.
    ... or ...
    010.112.156.211 in ip, and 60000 in port.
    The logical formula for the decode is as follows (from right to left):
    - last three bytes of string are the third IP octet.
    - prior byte is junk, and can be anything. Ignore.
    - prior three bytes of string are the fourth IP octet.
    - prior byte is junk, and can be anything. Ignore.
    - prior three bytes of string are the first IP octet.
    - prior byte is junk, and can be anything. Ignore.
    - prior three bytes of string are the second IP octet.
    - prior byte is junk, and can be anything. Ignore.
    - prior bytes (might be 4 or 5 in length) are the port.

    I can do it in a relatively straightforward way, assuming the string is always the same length. But I don't know an easy way of doing this with a string that might vary in size. Here's how I'm doing it, but of course it doesn't work for the shorter (first example) string...
    Code:
        f = buf;
        *(f + 5) = '\0';
    
        servers[x].port = atoi(f);
        f += 6;
    
        *(f + 3) = '\0';
        *(f + 7) = '\0';
        *(f + 11) = '\0';
    
        if (*(f + 4) == '0')
                strcpy(servers[x].ip, (f + 5));
        else
                strcpy(servers[x].ip, (f + 4));
    
        strcat(servers[x].ip, ".");
        strcat(servers[x].ip, (f + 0));
        strcat(servers[x].ip, ".");
        strcat(servers[x].ip, (f + 12));
        strcat(servers[x].ip, ".");
        strcat(servers[x].ip, (f + 8));
    Help (in the form of some code) would be greatly appreciated at this time... my mind is melting.

    Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It appears that the string has to be 20 or 21 bytes, not counting \n? (And I assume you mean \0, the string termination character.) In that case, use strlen to find out which it is -- that will tell you whether you need to use four or five for the beginning read; and the rest is gravy.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Thank you, tabstop... I just combined three lines into an if/else block and that took care of the beginning stuff (the port number) without having to re-engineer the rest of the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 10:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21