学习无止境!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

public static string NumToIp(long ipLong)
        {
            string strip = "";
            long ip1, ip2, ip3, ip4;

            ip1 = ipLong/16777216; //16777216=2^24
            ip2 = ipLong%16777216/65536; //65536=2^16
            ip3 = (ipLong%16777216%65536)/256; //256=2^8;
            ip4 = ipLong%16777216%65536%256;
            strip = ip1.ToString() + "." + ip2.ToString() + "." + ip3.ToString() + "." + ip4.ToString();
            return strip;
        }

        public static long IpToNum(string ip)
        {
            string[] ipList = ip.Split('.');
            long ipvalue = long.Parse(ipList[3]) + long.Parse(ipList[2])*256 + long.Parse(ipList[1])*256*256 +
                           long.Parse(ipList[0])*256*256*256;
            return ipvalue;
        }

        public static bool IsIP(string ip)
        {
            string pattrn =
                @"(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])";
            if (System.Text.RegularExpressions.Regex.IsMatch(ip, pattrn))
            {
                return true;
            }
            else
            {
                return
                    false;
            }
        }

posted on 2010-12-31 17:00  钻石眼泪  阅读(541)  评论(0编辑  收藏  举报