- public static long IP2Long(string ip)
- {
- string[] ipBytes;
- double num = 0;
- if(!string.IsNullOrEmpty(ip))
- {
- ipBytes = ip.Split('.');
- for (int i = ipBytes.Length - 1; i >= 0; i--)
- {
- num += ((int.Parse(ipBytes[i]) % 256) * Math.Pow(256, (3 - i)));
- }
- }
- return (long)num;
- }
- long start = IP2Long("127.0.0.1");
- long end = IP2Long("127.0.0.255");
- long ipAddress = IP2Long("127.0.1.253");
- bool inRange = (ipAddress >= start && ipAddress <= end);
- if (inRange){
- //IP Address fits within range!
- }
来源: http://www.phpxs.com/code/1004725/