架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 18839|回复: 3

[WinForm] c# 指定ip段生成ip地址

[复制链接]
发表于 2015-12-14 15:01:40 | 显示全部楼层 |阅读模式
QQ截图20151214150304.jpg

  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.         }  
复制代码






上一篇:C# WPF使用ZXing生成二维码ImageSource介绍
下一篇:二位字符数组的输入,输出时排序
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2015-12-14 15:54:44 | 显示全部楼层
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2015-12-31 16:25:19 | 显示全部楼层
上面的生成ip段地址有错误,用下面的方法吧

  1. #region ip转换
  2.         /// <summary>
  3.         /// ip转成long
  4.         /// </summary>
  5.         /// <param name="ip"></param>
  6.         /// <returns></returns>
  7.         public static long IpToInt(string ip)
  8.         {
  9.             char[] separator = new char[] { '.' };
  10.             string[] items = ip.Split(separator);
  11.             return long.Parse(items[0]) << 24
  12.                     | long.Parse(items[1]) << 16
  13.                     | long.Parse(items[2]) << 8
  14.                     | long.Parse(items[3]);
  15.         }
  16.         /// <summary>
  17.         /// long转成ip
  18.         /// </summary>
  19.         /// <param name="ipInt"></param>
  20.         /// <returns></returns>
  21.         public static string IntToIp(long ipInt)
  22.         {
  23.             StringBuilder sb = new StringBuilder();
  24.             sb.Append((ipInt >> 24) & 0xFF).Append(".");
  25.             sb.Append((ipInt >> 16) & 0xFF).Append(".");
  26.             sb.Append((ipInt >> 8) & 0xFF).Append(".");
  27.             sb.Append(ipInt & 0xFF);
  28.             return sb.ToString();
  29.         }
  30.         #endregion
复制代码


码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2016-1-2 15:11:56 | 显示全部楼层
学习了,谢谢分享
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-1-2 21:42

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表