查找mac地址是否在范围内的最有效方法是什么?

时间:2014-07-21 15:22:41

标签: vb.net ip

我有大量的字符串是mac地址。如果他们处于某个范围内,我希望能够进入某个案例陈述。

例如," 78:A1:83:24:00:00到78:A1:83:24:0F:FF"

我有我的代码,以便删除最后两个十六进制并选择off,如下所示:

 '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"

但这看起来像我在浪费空间。更好的方法是什么?

3 个答案:

答案 0 :(得分:2)

MAC地址只不过是以十六进制形式写的6个字节所代表的数字 因此,转换MAC地址的下限和上限可以为您提供可管理的值范围,以便使用单个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

现在,作为避免长长的IF列表的示例,您可以使用填充了范围的词典,然后应用简单的For Each逻辑来查找范围

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

答案 1 :(得分:1)

现有的功能没有提供这样的功能。您可以拆分每个字符串并手动创建范围。

答案 2 :(得分:0)

我会对各种范围使用某种正则表达式(正则表达式)验证。

http://regexlib.com/Search.aspx?k=mac%20address&AspxAutoDetectCookieSupport=1 会给你一个很好的起点。