In C#, assume that you have an IP address range represented as a string value:
"192.168.1.1-192.168.2.30"
and you also have a single IP address represented as a string value like:
"192.168.1.150"
What would be the most elegant way to determine if the address range contains the single IP address?
Cast the IP to 32bit integer (IP is 4 bytes, so it could be also represented as an integer). Than checking the range is simply checking if the given IP (int) is between two other IPs (2 other ints).
if( low_range <= checked_ip <= high_range ){ TRUE! }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With