例如1.0.1.0/24 这种格式转换成1.0.1.0 1.0.1.255
/// <summary>
///
/// </summary>
/// <param name="ipMask">格式为 ip/mask </param>
/// <param name="firstIp">起始ip</param>
/// <param name="lastIp">结束ip</param>
public bool DecodeNetmask(string ipMask, out string firstIp, out string lastIp)
{
firstIp = string.Empty;
lastIp = string.Empty;
string[] partsIpMask = Regex.Split(ipMask, @"/");
if (partsIpMask.Length < 2)
return false;
byte[] bip = IPAddress.Parse(partsIpMask[0]).GetAddressBytes();
string mask = IntToIp(Bit2Long(Convert.ToUInt16(partsIpMask[1])));
byte[] bsub = IPAddress.Parse(mask).GetAddressBytes();
for (int i = 0; i < bip.Length; i++)
{
bip[i] = (byte)((~bsub[i]) | bip[i]);
}
lastIp = new IPAddress(bip).ToString();
return true;
}
public static Int64 Bit2Long(UInt16 MaskBit)
{
//int a = 32 - MaskBit;
//long b = 0xffffffff << a;
//long c = b >> 0;
return (0xffffffff << (32 - MaskBit)) >> 0;
}
public static string IntToIp(long ipInt)
{
StringBuilder sb = new StringBuilder();
sb.Append((ipInt >> 24) & 0xFF).Append(".");
sb.Append((ipInt >> 16) & 0xFF).Append(".");
sb.Append((ipInt >> 8) & 0xFF).Append(".");
sb.Append(ipInt & 0xFF);
return sb.ToString();
}