ExtensionMethod.NET Home of 876 C#, Visual Basic, F# and Javascript extension methods

convert Internet dot address to network address

Csharp equivalent of Linux / Unix Command: inet_aton. The inet_aton() extension method converts the specified ipaddress, in the Internet standard dot notation, to a network address.

Source

public static double inet_aton(this IPAddress IPaddress)
{
    int i;
    double num = 0;
    if (IPaddress.ToString() == "")
    {
	return 0;
    }
    string[] arrDec = IPaddress.ToString().Split('.');
    for (i = arrDec.Length - 1; i >= 0; i--)
    {
	num += ((int.Parse(arrDec[i]) % 256) * Math.Pow(256, (3 - i)));
    }
    return num;
}

Example

IPAddress ipAddress = IPAddress.Parse(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
double ipNumber = ipAddress.inet_aton();

Author: DotNerd

Submitted on: 18 jun 2008

Language: C#

Type: System.Net

Views: 6023