如何每两个字符插入一个空格?

时间:2010-06-03 12:46:38

标签: string byte spaces autoit

我想将字节字符串(例如AAFF10DC)与空格分开,因此它变为AA FF 10 DC

如何在AutoIt(v3)中执行此操作?

3 个答案:

答案 0 :(得分:2)

  

我想用空格分割字节字符串......

使用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)

返回AA FF 10 DC

答案 1 :(得分:1)

这很难看,但它有效:

$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

答案 2 :(得分:1)

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

只是另一种方式。对于大量的字节数据,我不建议使用这种方法。