C#下的IP2Long
C#下的IP2Long
public ulong iptolong(string ip)
{
string[] cip = ip.Trim().Split('.');
string[] aip = new string[4];
cip.CopyTo(aip,0);
if (cip.Length < 3)
{
for (int i = 3;i>cip.Length; i--)
{
aip[i] = "0";
}
}
uint[] iip = new uint[4];
Regex reg=new Regex(@"\d+");
for (int x = 0; x < aip.Length; x++)
{
if (reg.IsMatch(aip[x]))
iip[x] = Convert.ToUInt32(aip[x]);
else
iip[x] = 0;
}
ulong uip = Convert.ToUInt64(256 * 256 * 256 * iip[0] + 256 * 256 * iip[1] + 256 * iip[2] + iip[3]);
return uip;
}
public ulong iptolong(string ip)
{
string[] cip = ip.Trim().Split('.');
string[] aip = new string[4];
cip.CopyTo(aip,0);
if (cip.Length < 3)
{
for (int i = 3;i>cip.Length; i--)
{
aip[i] = "0";
}
}
uint[] iip = new uint[4];
Regex reg=new Regex(@"\d+");
for (int x = 0; x < aip.Length; x++)
{
if (reg.IsMatch(aip[x]))
iip[x] = Convert.ToUInt32(aip[x]);
else
iip[x] = 0;
}
ulong uip = Convert.ToUInt64(256 * 256 * 256 * iip[0] + 256 * 256 * iip[1] + 256 * iip[2] + iip[3]);
return uip;
}
posted on 2007-12-25 14:43 William-ren 阅读(544) 评论(0) 编辑 收藏 举报