Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   converting to 32 bit long form (http://www.go4expert.com/forums/converting-32-bit-long-form-t7335/)

arvind_khadri 16Nov2007 19:52

converting to 32 bit long form
 
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....

Salem 16Nov2007 20:44

Re: converting to 32 bit long form
 
Do you have any examples of what you're trying to accomplish?

arvind_khadri 16Nov2007 23:14

Re: converting to 32 bit long form
 
if i enter 153.18.8.105 the o/p should be 2568095849....for separation u can use strtok for separating.....

Salem 17Nov2007 03:54

Re: converting to 32 bit long form
 
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.

arvind_khadri 17Nov2007 12:51

Re: converting to 32 bit long form
 
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.....

Salem 17Nov2007 13:46

Re: converting to 32 bit long form
 
You mean that
2568095849 is (153 << 24) + (18 << 16) + (8 << 8) + 105

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

arvind_khadri 18Nov2007 08:45

Re: converting to 32 bit long form
 
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...

arvind_khadri 18Nov2007 09:11

Re: converting to 32 bit long form
 
i wasnt able to sihft it...whenver i giver 153<<24 it shows 0 as o/p....

Salem 18Nov2007 13:28

Re: converting to 32 bit long form
 
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.

arvind_khadri 18Nov2007 17:40

Re: converting to 32 bit long form
 
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();
}



All times are GMT +5.5. The time now is 15:11.