Search code examples
stringbytespacesautoit

How to insert a space every two characters?


I would like to split byte strings, for example AAFF10DC, with spaces, so it becomes AA FF 10 DC.

How to do this in AutoIt (v3)?


Solution

  • This is sorta ugly, but it works:

    $string = "AAFF10DC"
    
    $strArray = StringSplit($string, "") ; No delimiter will separate all chars.
    
    $strResult = ""
    
    If IsEvenNumber($strArray[0]) Then
    
        For $i = 1 to $strArray[0] Step 2
            $strResult = $strResult & $strArray[$i] & $strArray[$i+1] & " "
        Next
    
        MsgBox(0, "Result", $strResult)
    
    Else
        MsgBox(0, "Result", "String does not contain an even number of characters.")
    EndIf
    
    Func IsEvenNumber($num)
        Return Mod($num, 2) = 0
    EndFunc