B4J Code Snippet Next/Previous MAC Address

I needed to work out the next or previous MAC address for a given MAC address. Put together these two subs (inspired by this thread)

B4X:
Sub GetNextMacAddress(MacAddress As String) As String
    Dim strHexParts() As String
    Dim intPart As Int
    Dim i As Int

    strHexParts = Regex.split(":",MacAddress.ToUpperCase)
   
    For i = strHexParts.Length-1 To 0 Step -1
        intPart = Bit.ParseInt(strHexParts(i), 16) + 1
       
        If intPart < 256 Then
            strHexParts(i) = Bit.ToHexString(intPart).ToUpperCase
            If strHexParts(i).Length = 1 Then strHexParts(i) = "0" & strHexParts(i)
            Exit
        Else
            strHexParts(i) = "00"
        End If
    Next
   
    Dim Result As String = ""
   
    For i = 0 To strHexParts.Length-1
        If Result.Length > 0 Then Result = Result & ":"
        Result = Result & strHexParts(i)
    Next
   
    Return Result
End Sub

Sub GetPreviousMacAddress(MacAddress As String) As String
    Dim strHexParts() As String
    Dim intPart As Int
    Dim i As Int

    strHexParts = Regex.split(":",MacAddress.ToUpperCase)
   
    For i = strHexParts.Length-1 To 0 Step -1
        intPart = Bit.ParseInt(strHexParts(i), 16) - 1
       
        If intPart >= 0 Then
            strHexParts(i) = Bit.ToHexString(intPart).ToUpperCase
            If strHexParts(i).Length = 1 Then strHexParts(i) = "0" & strHexParts(i)
            Exit
        Else
            strHexParts(i) = "FF"
        End If
    Next
   
    Dim Result As String = ""
   
    For i = 0 To strHexParts.Length-1
        If Result.Length > 0 Then Result = Result & ":"
        Result = Result & strHexParts(i)
    Next
   
    Return Result
End Sub
 
Top