469,287 Members | 2,746 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

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.2568095849) using strtok library function and unions.
Aug 13 '07 #1
14 17216
Meetee
931 Expert Mod 512MB
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.2568095849) using strtok library function and unions.
Hi,

Please read posting guidelines

Hope this link helps to achieve your problem solution.

Regards
Aug 13 '07 #2
gpraghuram
1,275 Expert 1GB
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
Aug 13 '07 #3
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
Aug 13 '07 #4
Am Learning now Pls help me..
Aug 14 '07 #5
sicarie
4,677 Expert Mod 4TB
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...
Aug 14 '07 #6
JosAH
11,448 Expert 8TB
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
Aug 14 '07 #7
hello, pls look in to my program
and help me to modify this....
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>
  4.  
  5.  main()
  6. {
  7.         int x = 1,z;
  8.         long l,k;
  9.         char str[]=" 153.18.8.105";
  10.         char *str1;
  11.         char str2[16];
  12.  
  13.         printf("String: %s\n", str);
  14.         str1 = strtok(str, ".");
  15.         strcpy(str2,str1);
  16.  
  17.         while (1)
  18.       {
  19.  
  20.             str1 = strtok(NULL, ".");
  21.  
  22.  
  23.                 if (str1 == NULL)
  24.                  {
  25.  
  26.                         exit(0);
  27.                  }
  28. strcat(str2,str1);
  29. l=atol(str2);
  30. printf("long integer is %ld \n",l);
  31.        }
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
Aug 27 '07 #8
sicarie
4,677 Expert Mod 4TB
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.
Aug 27 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 27 '07 #10
I was asked to do with structure/union....what to do....how to modify pls help
Aug 29 '07 #11
vinot85
53
Why should you go for strtok() to convert those IP addresses?
You can just make use of htonl() and htons()..

regards,
Vinoth
Aug 30 '07 #12
sicarie
4,677 Expert Mod 4TB
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.
Aug 30 '07 #13
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.2568095849) 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.
Aug 31 '07 #14
sicarie
4,677 Expert Mod 4TB
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.
Aug 31 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Joseph Suprenant | last post: by
3 posts views Thread by Golan | last post: by
18 posts views Thread by Martin Pöpping | last post: by
65 posts views Thread by kyle.tk | last post: by
1 post views Thread by UKuser | last post: by
14 posts views Thread by Spitfire | last post: by
4 posts views Thread by Alan | last post: by
1 post views Thread by kakolibora | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.