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());
        }
posted @ 2007-10-31 11:10  RobotTech  阅读(1654)  评论(0编辑  收藏  举报