IP地址转数字
public long IPToNumber(string ip)
{
string[] arrayIP = ip.Split('.');
long sip1 = long.Parse(arrayIP[0]);
long sip2 = long.Parse(arrayIP[1]);
long sip3 = long.Parse(arrayIP[2]);
long sip4 = long.Parse(arrayIP[3]);
long ipNum = sip1 * 256 * 256 * 256 + sip2 * 256 * 256 + sip3 * 256 + sip4;
return ipNum;
}
数字转IP地址
public string NumberToIP(long ipNum)
{
StringBuilder builder = new StringBuilder();
builder.Append((ipNum >> 24) & 0xFF).Append(".");
builder.Append((ipNum >> 16) & 0xFF).Append(".");
builder.Append((ipNum >> 8) & 0xFF).Append(".");
builder.Append(ipNum & 0xFF);
return builder.ToString();
}