开发者

How to insert a space every two characters?

开发者 https://www.devze.com 2023-01-02 07:53 出处:网络
I would like to split byte strings, for exampl开发者_StackOverflow社区e AAFF10DC, with spaces, so it becomes AA FF 10 DC.

I would like to split byte strings, for exampl开发者_StackOverflow社区e AAFF10DC, with spaces, so it becomes AA FF 10 DC.

How to do this in AutoIt (v3)?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消