Converting IP Address to long int with strtok()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • punithavinod
    New Member
    • Aug 2007
    • 5

    Converting IP Address to long int with strtok()

    Please send a program in c..which aceppts the IP address in the form(for ex: 153.18.8.105) and converts it into 32bit long interger(ex.256 8095849) using strtok library function and unions.
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by punithavinod
    Please send a program in c..which aceppts the IP address in the form(for ex: 153.18.8.105) and converts it into 32bit long interger(ex.256 8095849) using strtok library function and unions.
    Hi,

    Please read posting guidelines

    Hope this link helps to achieve your problem solution.

    Regards

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Hi,
      Instead of blind posting ur questions try to get the idea about how inet_addr works and try to understand strtok to write the code.

      Raghuram

      Comment

      • pntkiran
        New Member
        • Jun 2007
        • 16

        #4
        Hi ,
        Sample code to convert ip address from string to Long. But for better solution try to use the in built function.

        Code snipped - please refrain from spoonfeeding.

        Regards
        Kin Parmar

        Comment

        • punithavinod
          New Member
          • Aug 2007
          • 5

          #5
          Am Learning now Pls help me..

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by punithavinod
            Am Learning now Pls help me..
            Did you read zodilla58 and gpraghuram's responses? Did you look through the links, and did you read the Posting Guidelines of this site? Zodilla has already offered a link to the resource you need, why not look through that and start playing around with it? It's the best way to learn. Then when you have a more specific question, you can post again...

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by punithavinod
              Am Learning now Pls help me..
              We don't consider copying and pasting spoonfed code 'learning' ... please give it
              a try yourself first and when you're stuck feel free to ask specific questions here;
              we're willing to help you then.

              kind regards,

              Jos

              Comment

              • punithavinod
                New Member
                • Aug 2007
                • 5

                #8
                hello, pls look in to my program
                and help me to modify this....
                [code=c]
                #include <stdio.h>
                #include <string.h>
                #include<stdlib .h>

                main()
                {
                int x = 1,z;
                long l,k;
                char str[]=" 153.18.8.105";
                char *str1;
                char str2[16];

                printf("String: %s\n", str);
                str1 = strtok(str, ".");
                strcpy(str2,str 1);

                while (1)
                {

                str1 = strtok(NULL, ".");


                if (str1 == NULL)
                {

                exit(0);
                }
                strcat(str2,str 1);
                l=atol(str2);
                printf("long integer is %ld \n",l);
                }[/code]
                here i tried some what......the thing is i dont knoe structure and union...
                how to get the input string from user i.e the ip address using struct/union..pls guide me
                Last edited by sicarie; Aug 27 '07, 12:54 PM. Reason: Code Tags.

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Originally posted by zodilla58
                  Hi,

                  Please read posting guidelines

                  Hope this link helps to achieve your problem solution.

                  Regards
                  Reading through things other people have posted will save you quite a bit of time. Why not use the library available to you?

                  Why do you need to use a union or struct? We need a better explanation of what and how you are trying to do before we can give you more help.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    A lotta folks just keep using strtok(). I want to say that strtok() works because it uses a static variable. That static variable makes strtok() a disaster in a multi-threaded program where the same strtok() could be simultaneously called on separate threads creating a race condition.

                    Any progam using static or global variables is not thread safe and has to remain a single-threaded program.

                    This is a problem since most modern software is multithreaded.

                    Just keep this in mind when you use this thing.

                    Comment

                    • punithavinod
                      New Member
                      • Aug 2007
                      • 5

                      #11
                      I was asked to do with structure/union....what to do....how to modify pls help

                      Comment

                      • vinot85
                        New Member
                        • Aug 2007
                        • 53

                        #12
                        Why should you go for strtok() to convert those IP addresses?
                        You can just make use of htonl() and htons()..

                        regards,
                        Vinoth

                        Comment

                        • sicarie
                          Recognized Expert Specialist
                          • Nov 2006
                          • 4677

                          #13
                          Originally posted by vinot85
                          Why should you go for strtok() to convert those IP addresses?
                          You can just make use of htonl() and htons()..

                          regards,
                          Vinoth
                          I was unaware of that library when I suggested srtok(), it would be much easier to use those, if the OP's assignment allows for them - they have yet to clarify why they need to use a union.

                          Comment

                          • vishu89
                            New Member
                            • Aug 2007
                            • 1

                            #14
                            Originally posted by punithavinod
                            Please send a program in c..which aceppts the IP address in the form(for ex: 153.18.8.105) and converts it into 32bit long interger(ex.256 8095849) using strtok library function and unions.

                            i wil tell u the logic. u have to implement. using strtok u make seperate strings and then u use atoi function and convert it to integer. Then convert each integer to bianry. then concatenete all four binary nos. then convert that whole binary no into decimal no. that gives u the 32-bit long int.

                            Comment

                            • sicarie
                              Recognized Expert Specialist
                              • Nov 2006
                              • 4677

                              #15
                              Originally posted by vishu89
                              i wil tell u the logic. u have to implement. using strtok u make seperate strings and then u use atoi function and convert it to integer. Then convert each integer to bianry. then concatenete all four binary nos. then convert that whole binary no into decimal no. that gives u the 32-bit long int.
                              That was what I was originally thinking as well, but it would be easier for the OP to include the above library and use one function to 'just do it' and not re-implement something that is already out there.

                              Comment

                              Working...