What's the most efficient way to find if a mac address is in range?

Kat

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?

Steve

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What's the most efficient way to filter a DataFrame

From Dev

What's the most efficient way to sleep in Python?

From Dev

What is the most efficient way to obtain an ordered range using EntityFramework?

From Dev

What's the most efficient way to find matching sequences of cells in two (or more) arrays?

From Dev

What is the most efficient way to find the first matching record?

From Dev

What is most efficient way to find the intersection of a line and a circle in python?

From Dev

What is the most efficient way to find out which subnet an IP belongs to

From Dev

What is the most efficient way to find if something is repeated in a list?

From Dev

What is the most efficient way to find out which subnet an IP belongs to

From Dev

What is the most efficient way to find if something is repeated in a list?

From Dev

What's the most efficient way to search a list millions of times?

From Dev

What's the most efficient way to copy and paste brackets and their contents in vim?

From Dev

What's the most efficient way to read a very large file in chunks?

From Dev

What's the most efficient way to decode a UTF16 binary?

From Dev

What's the most efficient way to search a set in c++

From Dev

What's the most efficient way to evaluate if a string is a palindrome using Javascript?

From Dev

What's the most efficient way to reuse an iterator in Rust?

From Dev

What is the most efficient way to create nested div's in Javascript?

From Dev

What's the most efficient way of reading relations out of a pouchdb database

From Dev

What's the most efficient way to insert an element into a sorted vector?

From Dev

What's the most efficient way to save data in MySQL Database

From Dev

What's the most efficient way to accumulate dataframes in pyspark?

From Dev

What's the most efficient way to visit a .html page?

From Dev

What's the most efficient way to sort a Text value?

From Dev

What's the most efficient way to parse these 500k rows?

From Dev

What's the most efficient way of retrieving a batch of keys from an IndexedRDD

From Dev

What's the most efficient way of coding an animated header for a webpage?

From Dev

What's the most efficient way of operating with fields in MySQL?

From Dev

What's the most efficient way to use Angular component/directive bindings?

Related Related

  1. 1

    What's the most efficient way to filter a DataFrame

  2. 2

    What's the most efficient way to sleep in Python?

  3. 3

    What is the most efficient way to obtain an ordered range using EntityFramework?

  4. 4

    What's the most efficient way to find matching sequences of cells in two (or more) arrays?

  5. 5

    What is the most efficient way to find the first matching record?

  6. 6

    What is most efficient way to find the intersection of a line and a circle in python?

  7. 7

    What is the most efficient way to find out which subnet an IP belongs to

  8. 8

    What is the most efficient way to find if something is repeated in a list?

  9. 9

    What is the most efficient way to find out which subnet an IP belongs to

  10. 10

    What is the most efficient way to find if something is repeated in a list?

  11. 11

    What's the most efficient way to search a list millions of times?

  12. 12

    What's the most efficient way to copy and paste brackets and their contents in vim?

  13. 13

    What's the most efficient way to read a very large file in chunks?

  14. 14

    What's the most efficient way to decode a UTF16 binary?

  15. 15

    What's the most efficient way to search a set in c++

  16. 16

    What's the most efficient way to evaluate if a string is a palindrome using Javascript?

  17. 17

    What's the most efficient way to reuse an iterator in Rust?

  18. 18

    What is the most efficient way to create nested div's in Javascript?

  19. 19

    What's the most efficient way of reading relations out of a pouchdb database

  20. 20

    What's the most efficient way to insert an element into a sorted vector?

  21. 21

    What's the most efficient way to save data in MySQL Database

  22. 22

    What's the most efficient way to accumulate dataframes in pyspark?

  23. 23

    What's the most efficient way to visit a .html page?

  24. 24

    What's the most efficient way to sort a Text value?

  25. 25

    What's the most efficient way to parse these 500k rows?

  26. 26

    What's the most efficient way of retrieving a batch of keys from an IndexedRDD

  27. 27

    What's the most efficient way of coding an animated header for a webpage?

  28. 28

    What's the most efficient way of operating with fields in MySQL?

  29. 29

    What's the most efficient way to use Angular component/directive bindings?

HotTag

Archive