Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ParseIpRanges invalid character in string
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here //var x = Environment.CurrentDirectory; Console.WriteLine(IsIpInRange("67.63.175.193", "67.63.175.254", "67.63.175.205", 2)); ParseIpRanges(); } /// <summary> /// /// </summary> /// <param name="lowRange"></param> /// <param name="highRange"></param> /// <param name="mode">default is 2, 1 = exclude lower end but include upper end, 2= include both upper and lower end, 3 = exclude both low and high boundaries, 4= include lower but exclude upper end</param> /// <returns></returns> public static bool IsIpInRange(string lowRange, string highRange, string _ipAddress, int mode = 2) { var lowerRange = Ip2Long(lowRange); var upperRange = Ip2Long(highRange); var ipAddress =Ip2Long(_ipAddress); switch (mode) { case 1: return ipAddress > lowerRange && ipAddress <= upperRange; case 3: return ipAddress > lowerRange && ipAddress < upperRange; case 4: return ipAddress >= lowerRange && ipAddress < upperRange; case 2: default: return ipAddress >= lowerRange && ipAddress <= upperRange; } } 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; } /// <summary> /// /// </summary> /// <param name="itm">IP Range Setting Folder</param> /// <returns></returns> private static void ParseIpRanges() { var retLst = new List<Tuple<long, long>>(); var ipRanges = new string[]{"67.63.175.205–67.63.175.205"}; for (var i = 0; i < ipRanges.Length; i++) { if (!string.IsNullOrWhiteSpace(ipRanges[i])) { var range = ipRanges[i].Split("-".ToCharArray()); Console.WriteLine(range[0]); try { var lowerRange = Ip2Long(range[0]); var upperRange = Ip2Long(range[1]); Console.WriteLine(lowerRange); Console.WriteLine(upperRange); } catch (Exception e) { Console.WriteLine(e); //If cannot parse range, than skip and log //Log.Error("Could not parse IPRange:"+ipRanges[i],e,typeof(IDMAuthenticationHelper)); } } } } } }
run
|
edit
|
history
|
help
0
yarirey
Practicando C#
ElaineBrown**
https://www.facebook.com/gluncev
凑单
Find the sum of squares for prime numbers of the form (4k + 1).
Remove duplicates in a linked list
Lesson 2
Fórum Parallel Threads ( With Much Fun )
FindElementInAscendingSortedRotatedArray