码迷,mamicode.com
首页 > Windows程序 > 详细

C# 指定ip段生成ip地址

时间:2016-11-24 08:02:58      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:event   ip地址   split   box   app   private   textbox   code   byte   

private void button1_Click(object sender, EventArgs e)  
        {  
            string StartIp = "";  
            string EndIp = "";  
            StartIp = Startinput.Text;  
            EndIp = Endinput.Text;  
            uint iStartip = ipTint(StartIp);  
            uint iEndIp = ipTint(EndIp);  
            //string ip_result="";  
            StringBuilder ip_result=new StringBuilder();  
            if (iEndIp >= iStartip)  
            {  
                for (uint ip = iStartip; ip <= iEndIp; ip++)  
                {  
                    ip_result.Append(intTip(ip)).Append("\r\n");  
                    //ip_result = ip_result + intTip(ip)+"\r\n";  
                }  
                resultTextBox.Text = ip_result.ToString();   //RichTextBox  
            }  
            else  
            {  
                MessageBox.Show("天啊,起始ip居然比终止ip还大");  
            }  
        }  
        public static uint ipTint(string ipStr)  
        {  
            string[] ip = ipStr.Split(.);  
            uint ipcode = 0xFFFFFF00 | byte.Parse(ip[3]);  
            ipcode = ipcode & 0xFFFF00FF | (uint.Parse(ip[2]) << 0x8);  
            ipcode = ipcode & 0xFF00FFFF | (uint.Parse(ip[1]) << 0xF);  
            ipcode = ipcode & 0x00FFFFFF | (uint.Parse(ip[0]) << 0x18);  
            return ipcode;  
        }  
        public static string intTip(uint ipcode)  
        {  
            byte a = (byte)((ipcode & 0xFF000000) >> 0x18);  
            byte b = (byte)((ipcode & 0x00FF0000) >> 0xF);  
            byte c = (byte)((ipcode & 0x0000FF00) >> 0x8);  
            byte d = (byte)(ipcode & 0x000000FF);  
            string ipStr = string.Format("{0}.{1}.{2}.{3}", a, b, c, d);  
            return ipStr;  
        }  

 

C# 指定ip段生成ip地址

标签:event   ip地址   split   box   app   private   textbox   code   byte   

原文地址:http://www.cnblogs.com/testsec/p/6095658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!