I have a large range of number of strings that are mac addresses. If they're in a certain range, I want to be able to go to a certain case statement.
For example, "78:A1:83:24:00:00 to 78:A1:83:24:0F:FF"
I have my code going so that it removes the last two hex and selects off that such as below:
'remove last three characters
mac = mac.TrimEnd()
mac = mac.Substring(0, mac.Length - 3)
'convert
Select Case mac
'Advidia
Case "78:A1:83:40:00", "78:A1:83:40:01", "78:A1:83:40:02", "78:A1:83:40:03", _
"78:A1:83:40:04", "78:A1:83:40:05", "78:A1:83:40:06", "78:A1:83:40:07", _
"78:A1:83:40:08", "78:A1:83:40:09", "78:A1:83:40:0A", "78:A1:83:40:0B"
' it continues
Return "VP-1"
But this just looks like I'm wasting space. What's a better way to this?
A MAC address is nothing more than a number represented by 6 bytes written in hexdecimal form.
So, converting the lower and upper limit of the MAC address could give you a manageable range of values to check with a single IF
Sub Main
Dim lowRange = "78:A1:83:24:40:00"
Dim upRange = "78:A1:83:24:40:FF"
Dim startVal = GetValue(lowRange)
Dim endVal = GetValue(upRange)
Console.WriteLine(startVal) ' 132635085258752
Console.WriteLine(endVal) ' 132635085259007
Dim macToCheck = "78:A1:83:24:40:B0"
Dim checkVal = GetValue(macToCheck)
Console.WriteLine(checkVal) ' 132635085258928
if checkVal >= startVal AndAlso checkVal <= endVal Then
' VP-1
Console.WriteLine("In range")
End If
End SUb
Function GetValue(mac as String ) as Long
Dim clearText = mac.Replace(":", "")
Dim result
Long.TryParse(clearText, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, result)
return result
End Function
Now, as an example to avoid a long list of IF you could use a Dictionary filled with your ranges and then apply a simple For Each logic to find your ranges
Sub Main
Dim dc as New Dictionary(Of String, MacRange)()
dc.Add("VP-1", new MacRange() with
{
.Lower = "78:A1:83:24:40:00",
.Upper = "78:A1:83:24:40:FF"
})
dc.Add("VP-2", new MacRange() with
{
.Lower = "78:A1:83:24:41:00",
.Upper = "78:A1:83:24:41:FF"
})
dc.Add("VP-3", new MacRange() with
{
.Lower = "78:A1:83:24:42:00",
.Upper = "78:A1:83:24:42:FF"
})
Dim result = ""
Dim macToCheck = "78:A1:83:24:42:B0"
Dim checkVal = GetValue(macToCheck)
'For Each k in dc
' Dim lower = GetValue(k.Value.Lower)
' Dim upper = GetValue(k.Value.Upper)
' if checkVal >= lower AndAlso checkVal <= upper Then
' result = k.Key
' Exit For
' End If
'Next
'Console.WriteLine(result) ' VP-3
' The loop above could be replaced by this LINQ expression
Dim m = dc.FirstOrDefault(Function(x) checkVal >= GetValue(x.Value.Lower) AndAlso _
checkVal <= GetValue(x.Value.Upper))
If m IsNot Nothing Then
Console.WriteLine(m.Key) ' VP-3
Else
Console.WriteLine("Range not found")
End If
End Sub
Class MacRange
Public Lower as String
Public Upper as String
End Class
Function GetValue(mac as String ) as Long
Dim clearText = mac.Replace(":", "")
Dim result
Long.TryParse(clearText, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, result)
return result
End Function
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments