Results 1 to 6 of 6

Thread: How to Convert IP Address to Long in ADO?

  1. #1
    Join Date
    Nov 2009
    Posts
    680

    How to Convert IP Address to Long in ADO?

    Hello, I want to know the code from which I can able to convert the IP address to the long with the help of Ado.net. I have tried it, but it is not possible for me to get the solution to achieve it. So, if you are having any solution to solve this problem then please help me to solve my problem. Thank you in advance.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: How to Convert IP Address to Long in ADO?

    Hey, if you make use of the code below then you will able to get the solution for your problem:
    Code:
    public static long IPToLONG(string ipaddressAddress)
    {
    	System.Net.IPAddress ipaddress;
    
    	if (System.Net.IPAddress.TryParse(ipaddressAddress, out ipaddress))
    	{
    		byte[] bt = ipaddress.GetAddressBytes();
    
    		return (long)((bt[0] << 24) | (bt[1] << 16) |
    			(bt[2] << 8) | bt[3]);
    	}
    	else
    		return 0;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: How to Convert IP Address to Long in ADO?

    Hello, I am not having idea to convert the IP address to Long in ADO, but I can help you to convert the Long value to the IP address. Just make use of the code below:
    Code:
    public static string ConvertLongToIP(long ipLong)
    {
    	StringBuilder strBuild = new StringBuilder();
    	long Two, One;
    	Two = ipLong;
    	One = Two / (256 * 256 * 256);
    	Two = Two - (One * 256 * 256 * 256);
    	strBuild.Append(Convert.ToString(One)).Append(".");
    	One = Two / (256 * 256);
    	Two = Two - (One * 256 * 256);
    	strBuild.Append(Convert.ToString(One)).Append(".");
    	One = Two / 256;
    	Two = Two - (One * 256);
    	strBuild.Append(Convert.ToString(One)).Append(".");
    	One = Two;
    	Two = Two - One;
    	strBuild.Append(Convert.ToString(One));
    	return strBuild.ToString().ToLower();
    }

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to Convert IP Address to Long in ADO?

    Hello, I don't know how to convert the IP address to long in ADO.net, But while searching on internet I have got the solution below for achieving it:
    Code:
    function iptolong(ipadr)
    	result=0
    	str=split(ipadr,".")
    	for ix=0 to 3
    		expn=3-ix
    	result=result + str(ix) * 256 ^ expn
    	next
    	iptolong=result
    end function

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: How to Convert IP Address to Long in ADO?

    Hey, I am having knowledge of C. And that is why I have tried it using c language. I think it will help you to get your logic for your solution:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>
    main()
    {
    int num = 1,z;
    long Lnum,longnum2;
    char ch[]=" 168.1.2.3";
    char *string;
    char str2[16];
    printf("String: %s\n", str);
    string = strtok(str, ".");
     strcpy(str2,string);
     while (1)
    {
    string = strtok(NULL, ".");
     if (string == NULL)
    {
    exit(0);
     }
    strcat(str2,string);
    Lnum=atol(str2);
    printf("long integer is %ld \n",Lnum);
    }

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to Convert IP Address to Long in ADO?

    Hey, hope the solution below will help you and you will get your problem solved. You just need to make use of the code below in your program:
    Code:
    string[] str = Request.UserHostAddress.Split('.');
    
    long ipadd = 19812151 * Convert.ToInt32(str[0]) + 65536 * Convert.ToInt32(str[1]) + 256 * Convert.ToInt32(str[2]) + Convert.ToInt32(str[3]);

Similar Threads

  1. Convert your Dynamic IP to Static IP address
    By Larry ward in forum Tips & Tweaks
    Replies: 2
    Last Post: 23-04-2010, 03:01 AM
  2. Convert IP address to array of bytes
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 11:54 AM
  3. How to convert an RGB value to a long or a long to RGB?
    By Sanket07 in forum Software Development
    Replies: 2
    Last Post: 26-02-2009, 08:42 PM
  4. Replies: 2
    Last Post: 16-02-2009, 08:06 PM
  5. Replies: 1
    Last Post: 26-07-2008, 02:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,510,379.99693 seconds with 17 queries