Sign in to follow this  
Followers 0
Ptifou78

Reverse string by two letter

6 posts in this topic

#1 ·  Posted (edited)

Hi all today i would like to reverse a hex string(not really reverse) I just want a function tu translate 1 CD EA 29 into 29 EA CD 01

Thanks in advance :D

Edited by Ptifou78

Share this post


Link to post
Share on other sites



#4 ·  Posted (edited)

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

thanks a lot UEZ it's work perfectly :D

Share this post


Link to post
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0