zoukankan      html  css  js  c++  java
  • IP格式检查、IP INT 转换

     public static bool CheckIPAddress(string IP)
            {
                
    string pattern = @"(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))";
                System.Text.RegularExpressions.Regex regex 
    = new System.Text.RegularExpressions.Regex(pattern);
                
    if (regex.Match(IP).Success)
                {
                    
    return true;
                }
                
    else
                {
                    
    return false;
                }
            }

            
    private static long IpToInt(string ip)
            {
                
    char[] separator = new char[] { '.' };
                
    if (ip.Split(separator).Length == 3)
                {
                    ip 
    = ip + ".0";
                }
                
    string[] strArray = ip.Split(separator);
                
    long num2 = ((long.Parse(strArray[0]) * 0x100L* 0x100L* 0x100L;
                
    long num3 = (long.Parse(strArray[1]) * 0x100L* 0x100L;
                
    long num4 = long.Parse(strArray[2]) * 0x100L;
                
    long num5 = long.Parse(strArray[3]);
                
    return (((num2 + num3) + num4) + num5);
            }

            
    private static string IntToIP(long ip_Int)
            {
                
    long num = (long)((ip_Int & 0xff000000L>> 0x18);
                
    if (num < 0L)
                {
                    num 
    += 0x100L;
                }
                
    long num2 = (ip_Int & 0xff0000L>> 0x10;
                
    if (num2 < 0L)
                {
                    num2 
    += 0x100L;
                }
                
    long num3 = (ip_Int & 0xff00L>> 8;
                
    if (num3 < 0L)
                {
                    num3 
    += 0x100L;
                }
                
    long num4 = ip_Int & 0xffL;
                
    if (num4 < 0L)
                {
                    num4 
    += 0x100L;
                }
                
    return (num.ToString() + "." + num2.ToString() + "." + num3.ToString() + "." + num4.ToString());
            }
  • 相关阅读:
    团队冲刺第二天
    电梯演讲的准备——冰淇淋第一个项目NABCD分析
    团队冲刺第四天
    团队冲刺第六天
    团队冲刺第三天
    团队冲刺第一天
    XmlDocument类的WriteContentTo和WriteTo方法
    从一场DOTA对战中发现的哲理,也做为对2012年的展望
    String.Trim()真相大揭秘
    SQL Server 2008数据库维护计划
  • 原文地址:https://www.cnblogs.com/RobotTech/p/944053.html
Copyright © 2011-2022 走看看