public static string NumToIp(long ipLong)
{
string strip = "";
long ip1, ip2, ip3, ip4;
ip1 = ipLong/16777216; //16777216=2^24
ip2 = ipLong%16777216/65536; //65536=2^16
ip3 = (ipLong%16777216%65536)/256; //256=2^8;
ip4 = ipLong%16777216%65536%256;
strip = ip1.ToString() + "." + ip2.ToString() + "." + ip3.ToString() + "." + ip4.ToString();
return strip;
}
public static long IpToNum(string ip)
{
string[] ipList = ip.Split('.');
long ipvalue = long.Parse(ipList[3]) + long.Parse(ipList[2])*256 + long.Parse(ipList[1])*256*256 +
long.Parse(ipList[0])*256*256*256;
return ipvalue;
}
public static bool IsIP(string ip)
{
string pattrn =
@"(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])";
if (System.Text.RegularExpressions.Regex.IsMatch(ip, pattrn))
{
return true;
}
else
{
return
false;
}
}