converting to 32 bit long form

Discussion in 'C' started by arvind_khadri, Nov 16, 2007.

  1. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    hi...hey guys i just wanted a help on this....how do i convert a given number to a 32 bit long form...plz help....
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Do you have any examples of what you're trying to accomplish?
     
  3. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    if i enter 153.18.8.105 the o/p should be 2568095849....for separation u can use strtok for separating.....
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Use strtol().
    One of the things it returns is a pointer to where it got to (ie, the dots between numbers).
    This you use to locate the start of the next number.
     
  5. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    well salem i got tat part...but the next thing is how do i convert...tats wat am nt able to get...so i needed help on it.....
     
  6. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    You mean that
    2568095849 is (153 << 24) + (18 << 16) + (8 << 8) + 105

    It's just a loop, with a shift and and add.
     
  7. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    thanks for tat...well so rt nw u mean to say tat find the binary eqvt and keep shifting and then concatenate the thing to find final result??if u could post the code here it would be really very helpful of u...
     
  8. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    i wasnt able to sihft it...whenver i giver 153<<24 it shows 0 as o/p....
     
  9. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Well it would do, if you were storing the result in an unsigned char say. All the bits fell off the end!

    It needs to be in an unsigned long before you shift it left 24 bits.
     
  10. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    i could get the following code...but cant understand how it works.....can u pls explain it....
    code:cpp

    Code:
    #include<string.h>
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    union ipadd
     {
     unsigned long int a;
     } e;
    void main()
    {
    char ip[100];
    char *p;
    int i,n,j,po;
    unsigned long int d;
    e.a=0;
    clrscr();
    printf("\nenter the ip");
    scanf("%s",ip);
    p=strtok(ip,".");
    n=strlen(p) ;
    d=0;
    po=0;
    for(i=n-1;i>=0;i--)
     {
     d=d+(*(p+i)-48)*pow(10,po);
     po++;
     }
    e.a=d*pow(256,3);
    for(j=2;j>=0;j--)
     {
      p=strtok(NULL,".");
      n=strlen(p);
      d=0;
      po=0;
      for(i=n-1;i>=0;i--)
        {
         d=d+(*(p+i)-48)*pow(10,po);
         po++;
        }
      e.a=e.a+d*pow(256,j);
      }
    printf("\n the ip add is :%lu",e.a);
    getch();
    }
     
    Last edited by a moderator: Nov 18, 2007
  11. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    I could, but I'm not interested in reading code which has been posted without using code tags.
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    arvind_khadri, Learn to post code in the posts with the code block
     
  13. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    shabbir thanks for the code block....
    now its in code block...now pls help me....
     
  14. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Sure, there are lots of things wrong with it.

    > void main()
    http://c-faq.com/ansi/voidmain.html

    > #include<conio.h>
    This is a non-standard header file, so you should avoid using unless necessary.
    It is also obsolete as well, as no new compiler will ever have such a thing. Even compilers like dev-c++ which do include it do only for backward compatibility.

    > union ipadd
    A union with only one member is kinda pointless, so it looks just like a struct.

    > d=d+(*(p+i)-48)*pow(10,po);
    You know, this is exactly what strtol() does, so why not use it?
    In fact using strtol() means you can avoid the messy strtok() function as well, which as a side-effect trashes the input string.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice