Reputation: 26324
I want to know whether a given mac address is in range of specified mac addresses.
For example , i defined begin and end address . If you provide any mac address with in this range then the API has to return true otherwise false.
Eg : 70-71-BC-90-0D-01 is in range and 70-71-BC-90-0D-FF is not in range.
Begin address: 70-71-BC-90-00-00
End address : 70-71-BC-90-0D-F7
Thanks
Upvotes: 3
Views: 1101
Reputation: 424983
Since 0-9 chars are "less than" A-Z chars, and all inputs are the same length, just use String comparison:
String beginAddress = "70-71-BC-90-00-00";
String endAddress = "70-71-BC-90-0D-F7";
if (macAddress.compareToIgnoreCase(beginAddress) >= 0
&& macAddress.compareToIgnoreCase(endAddress) <= 0)
// in range
Upvotes: 5
Reputation: 417512
To be completely universal with the ranges (and not just answer to this specific range), you can create BigInteger
instances, and compare them.
To create BigInteger
s, we'll look at the MAC addresses as the hexadecimal representations of the numbers.
The following implementation doesn't care about:
'-'
separator characters in MAC addressesThe Java code:
BigInteger min = new BigInteger(minAddr.replace("-",""), 16);
BigInteger max = new BigInteger(maxAddr.replace("-",""), 16);
BigInteger test = new BigInteger(testAddr.replace("-",""), 16);
boolean isInRange = min.compareTo(test) <= 0 && max.compareTo(test) >= 0;
If you need to check many MAC addresses, you can obviously cache the min
and max
BigInteger
s.
Upvotes: 5