Code
private static void getSubnetIP(string ip, string sm,out string beginIP,out string endIP)
{
/*
* ip / subnetmark
* 123.155.0.0/17
*
01111011.11110101.00000000.00000000
11111111.11111111.10000000.00000000
*
*
*/
int subnetMask = int.Parse(sm);
string tBeginIP = "";
string tEndIP = "";
string[] ipList = ip.Split('.');
string ipByteStr = "";
foreach (string item in ipList)
{
ipByteStr += Convert.ToString(int.Parse(item), 2).PadLeft(8, '0');
}
string rByteBeginIP = ipByteStr.Substring(0, subnetMask).PadRight(32, '0');
string rByteEndIP = ipByteStr.Substring(0, subnetMask).PadRight(32, '1');
for (int i = 0; i < 4; i++)
{
tBeginIP += Convert.ToInt32(rByteBeginIP.Substring(i * 8, 8), 2).ToString() + ".";
}
for (int j = 0; j < 4; j++)
{
tEndIP += Convert.ToInt32(rByteEndIP.Substring(j * 8, 8), 2).ToString() + ".";
}
beginIP = tEndIP.TrimEnd('.');
endIP = tBeginIP.TrimEnd('.');
}
private static void getSubnetIP(string ip, string sm,out string beginIP,out string endIP)
{
/*
* ip / subnetmark
* 123.155.0.0/17
*
01111011.11110101.00000000.00000000
11111111.11111111.10000000.00000000
*
*
*/
int subnetMask = int.Parse(sm);
string tBeginIP = "";
string tEndIP = "";
string[] ipList = ip.Split('.');
string ipByteStr = "";
foreach (string item in ipList)
{
ipByteStr += Convert.ToString(int.Parse(item), 2).PadLeft(8, '0');
}
string rByteBeginIP = ipByteStr.Substring(0, subnetMask).PadRight(32, '0');
string rByteEndIP = ipByteStr.Substring(0, subnetMask).PadRight(32, '1');
for (int i = 0; i < 4; i++)
{
tBeginIP += Convert.ToInt32(rByteBeginIP.Substring(i * 8, 8), 2).ToString() + ".";
}
for (int j = 0; j < 4; j++)
{
tEndIP += Convert.ToInt32(rByteEndIP.Substring(j * 8, 8), 2).ToString() + ".";
}
beginIP = tEndIP.TrimEnd('.');
endIP = tBeginIP.TrimEnd('.');
}