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

发布于 2024-09-04 10:22:48 字数 109 浏览 2 评论 0原文

我想用空格分割字节字符串,例如 AAFF10DC,这样它就变成 AA FF 10 DC

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

闻呓 2024-09-11 10:22:48

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

示例使用 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

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.

半寸时光 2024-09-11 10:22:48

这有点丑陋,但它有效:

$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

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
彼岸花似海 2024-09-11 10:22:48
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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文