例如:
源文件
allofchina 58.28.0.0/16
转化后
allofchina 58.28.0.0/16 974913536 974979071
974913536 是该网段的最小,即58.28.0.0
974979071 是该网段的最大,即58.28.255.255
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net;
namespace ConsoleApplication1 { class Program {
static UInt32 IPv4StringToInt32(string strIP) { IPAddress ipAddress; if (IPAddress.TryParse(strIP,out ipAddress)) { if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { byte[] bytes = ipAddress.GetAddressBytes(); return (UInt32)(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]); } } return 0; }
static void Main(string[] args) { string line; System.IO.StreamReader file = new System.IO.StreamReader("E:\\IP\\ipsect.txt"); //In_File
System.IO.StreamWriter file2 = new System.IO.StreamWriter("E:\\IP\\out.txt"); //Out_File
while ((line = file.ReadLine()) != null) { string OutString=line; //OutString.Replace('\n', '\0');
//line: allofchina 58.28.0.0/16
if(line.Length > 0) //排除空行
{ string[] temp = line.Split('\t'); //temp[0]: allofchina temp[1]: 58.28.0.0/16
{ string[] tempp = temp[1].Split('/'); //tempp[0]: 58.28.0.0 tempp[1]: 16
string ip = tempp[0]; UInt32 mask = byte.Parse(tempp[1]);
UInt32 mask_for_min = 0; UInt32 mask_for_max = 0; UInt32 bit=1; for (int i = 0; i < 32-mask;i++ ) { mask_for_max |= bit; bit <<= 1; } mask_for_min = ~mask_for_max;
UInt32 IP = IPv4StringToInt32(ip);
UInt32 min = IP & mask_for_min; UInt32 max = IP | mask_for_max;
Console.Write(mask.ToString()); Console.Write("\t"); Console.Write(mask_for_min.ToString()); Console.Write("\t"); Console.WriteLine(mask_for_max.ToString()); OutString+="\t"; OutString+=min.ToString(); OutString += "\t"; OutString += max.ToString(); file2.WriteLine(OutString); } } } file.Close(); file2.Close(); Console.ReadLine(); } } }
|
阅读(482) | 评论(0) | 转发(0) |