Question

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)?

Was it helpful?

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

OTHER TIPS

I would like to split byte strings … with spaces …

Example using StringRegExpReplace() :

Global Const $g_sString  = 'AAFF10DC'
Global Const $g_sPattern = '(.{2})'
Global Const $g_sReplace = '$1 '
Global Const $g_sResult  = StringRegExpReplace($g_sString, $g_sPattern, $g_sReplace)

ConsoleWrite($g_sResult & @CRLF)

Returns AA FF 10 DC.

Global $s_string = "AAFF10DC"
MsgBox(64, "Info", _str_bytesep($s_string))

Func _str_bytesep($s_str, $s_delim = " ")
    If Not (Mod(StringLen($s_str), 2) = 0) Then Return SetError(1, 0, "")
    Return StringRegExpReplace($s_str, "(..(?!\z))", "$1" & $s_delim & "")
EndFunc

Is just another way to do it. For huge amounts of byte data, I would not suggest using this method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top