Jump to content

Reverse string by two letter


 Share

Recommended Posts

Try this:

ConsoleWrite(HexStringReverse("1 CD EA 29") & @LF & @error & @LF)
ConsoleWrite(HexStringReverse("FF A 3 CA") & @LF & @error & @LF)
ConsoleWrite(HexStringReverse("1abcdef") & @LF & @error & @LF)

Func HexStringReverse($HexString, $delim= " ") ;coded by UEZ 2011
    If Not IsString($HexString) Then Return SetError(1, 0, 0) ;not a string
    If StringRegExpReplace($HexString, "(?i)[0-9a-fs]", "") Then Return SetError(2, 0, 0) ;not a hex string
    Local $aResult = StringSplit($HexString, " ")
    If Not @error Then
        $aResult = StringSplit(StringRegExpReplace($HexString, "b([w])b", "0$1"), " ", 2)
    Else
         If Mod(StringLen($HexString), 2) Then $HexString = "0" & $HexString
         $aResult = StringRegExp($HexString, ".{2}", 3)
    EndIf
     _ArrayReverse($aResult)
    Return _ArrayToString($aResult, $delim)
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Here are other methods.

#include<Array.au3>

Local $string = "1 c8 D A"

ConsoleWrite(HexReverse($string) & @LF)
; or
ConsoleWrite(HexReverse1($string) & @LF)

; #include<Array.au3> required
Func HexReverse($HexString)
    If Not String($HexString) Then Return SetError(1, 0, 0)
    Local $aResult = StringSplit(StringStripWS(StringRegExpReplace(StringUpper($HexString), "\b([[:xdigit:]])\b", "0\1"), 7), " ", 2)
    _ArrayReverse($aResult)
    Return _ArrayToString($aResult, " ")
EndFunc   ;==>HexReverse

; No includes needed
Func HexReverse1($HexString)
    If Not String($HexString) Then Return SetError(1, 0, 0)
    Local $aResult
    $HexString = StringStripWS(StringRegExpReplace($HexString, "\b([[:xdigit:]])\b", "0\1"), 8)
    For $i = 1 To StringLen($HexString) Step 2
        $aResult = StringMid($HexString, $i, 2) & " " & $aResult
    Next
    Return StringUpper($aResult)
EndFunc   ;==>HexReverse1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...