This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18566|Reply: 3

[WinForm] C# specifies the IP segment to generate the IP address

[Copy link]
Posted on 12/14/2015 3:01:40 PM | | | |


  1. private void button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             string StartIp = "";  
  4.             string EndIp = "";  
  5.             StartIp = Startinput.Text;  
  6.             EndIp = Endinput.Text;  
  7.             uint iStartip = ipTint(StartIp);  
  8.             uint iEndIp = ipTint(EndIp);  
  9.             //string ip_result="";  
  10.             StringBuilder ip_result=new StringBuilder();  
  11.             if (iEndIp >= iStartip)  
  12.             {  
  13.                 for (uint ip = iStartip; ip <= iEndIp; ip++)  
  14.                 {  
  15.                     ip_result.Append(intTip(ip)).Append("\r\n");  
  16.                     //ip_result = ip_result + intTip(ip)+"\r\n";  
  17.                 }  
  18.                 resultTextBox.Text = ip_result.ToString();   //RichTextBox  
  19.             }  
  20.             else  
  21.             {  
  22.                 MessageBox.Show("天啊,起始ip居然比终止ip还大");  
  23.             }  
  24.         }  
  25.         public static uint ipTint(string ipStr)  
  26.         {  
  27.             string[] ip = ipStr.Split('.');  
  28.             uint ipcode = 0xFFFFFF00 | byte.Parse(ip[3]);  
  29.             ipcode = ipcode & 0xFFFF00FF | (uint.Parse(ip[2]) << 0x8);  
  30.             ipcode = ipcode & 0xFF00FFFF | (uint.Parse(ip[1]) << 0xF);  
  31.             ipcode = ipcode & 0x00FFFFFF | (uint.Parse(ip[0]) << 0x18);  
  32.             return ipcode;  
  33.         }  
  34.         public static string intTip(uint ipcode)  
  35.         {  
  36.             byte a = (byte)((ipcode & 0xFF000000) >> 0x18);  
  37.             byte b = (byte)((ipcode & 0x00FF0000) >> 0xF);  
  38.             byte c = (byte)((ipcode & 0x0000FF00) >> 0x8);  
  39.             byte d = (byte)(ipcode & 0x000000FF);  
  40.             string ipStr = string.Format("{0}.{1}.{2}.{3}", a, b, c, d);  
  41.             return ipStr;  
  42.         }  
Copy code






Previous:C# WPF uses ZXing to generate QR codes Introduction to ImageSource
Next:The input of the two-digit character array is sorted when the output is made
Posted on 12/14/2015 3:54:44 PM |
 Landlord| Posted on 12/31/2015 4:25:19 PM |
If there is an error in the generated IP address above, use the following method



Posted on 1/2/2016 3:11:56 PM |
Learned, thank you for sharing
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com