Convert IP to Long in C#
long IP2Long(IPAddress addr)
{
	try
	{
		String[] ipbytes;
		double num = 0;
 
		ipbytes = addr.ToString().Split('.');
		for (int i = ipbytes.Length - 1; i >= 0; i--)
			num += ((int.Parse(ipbytes[i]) % 256) * Math.Pow(256, (3 - i)));
 
		return (long)num;
	}
	catch
	{
		return -1;
	}
}
Categories: C# Tags: , ,
%d bloggers like this: