VIP

[SOLVED] Binary Replace | Hex Replace

17 posts in this topic

#1 ·  Posted (edited)

Local $sHexToReplace = "0x200000200100200200200300200400200500200600200700200800200900201000202000203000204000205000206000207000208000209000200A00200B00200C00200D00200E00200F0020A00020B00020C00020D00020E00020F000"
ConsoleWrite("!> IN: " & $sHexToReplace & @CRLF)
$sHexToReplace = StringReplace($sHexToReplace, "00", "")
ConsoleWrite("!>OUT: " & $sHexToReplace & @CRLF)

 

Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites



Your requirements are not clearly defined, we have to guess what you exactly want to do
Assumption : you want to remove 4 hex digits after each group of 2 digits ('20 ')

$sHexToReplace = "0x200000200100200200200300200400200500200600200700200800200900201000202000203000204000205000206000207000208000209000200A00200B00200C00200D00200E00200F0020A00020B00020C00020D00020E00020F000"

$sHexToReplace = StringTrimLeft($sHexToReplace, 2)
$res = "0x" & StringRegExpReplace($sHexToReplace, '[[:xdigit:]]{2}\K[[:xdigit:]]{4}', "")
msgbox(0,"", $res)

 

Share this post


Link to post
Share on other sites

Thank mikell very much!

I  replace the hex string (2 characters) in hex code
If using stringreplace it replaces unlike hex editor, so it did damage hex code.

If split hex string with two characters each can be successfully replaced but slow?
or can be search under the even number and next number.

That was my thought, it really is crap.


Regards,
 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You need to pair 2 characters and test those:

eg:  when replacing 00 in 2000 with xx you want to compare character 1 with a length of 2 (20) and then character 3 with a length of 2.(00) ending up with 20xx.

You are now replacing the first 00 combo which are 2000 ending with 2xx0.

Jos

Edited by Jos
1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Local $sHexToReplace = "0x200000200100200200200300200400200500200600200700200800200900201000202000203000204000205000206000207000208000209000200A00200B00200C00200D00200E00200F0020A00020B00020C00020D00020E00020F000"
ConsoleWrite("+ IN: " & $sHexToReplace & @CRLF)
$sHexToReplace = _BinaryReplace($sHexToReplace, "00", "")
ConsoleWrite("!OUT: " & $sHexToReplace & @CRLF & @CRLF)


Func _BinaryReplace($sBinaryOrFilePath, $sSearch, $sReplace, $sOccurrence = 0)
    If $sOccurrence = Default Or $sOccurrence = -1 Then $sOccurrence = 0
    Local $sBinary = $sBinaryOrFilePath
    Local $sReturn = 1
    Local $hOpen, $bSplit, $i, $sExtended
    If FileExists($sBinaryOrFilePath) Then
        $hOpen = FileOpen($sBinaryOrFilePath, 16)
        If @error Then Return SetError(2, 0, "");Open file error
        $sBinary = FileRead($hOpen)
        If @error Then Return SetError(3, 0, ""); Read file error
        FileClose($hOpen)
        $sReturn = 0
    Else
        If (StringLen($sBinaryOrFilePath) < 1) Or (StringLeft($sBinaryOrFilePath, 2) <> "0x") Or (StringLen($sSearch) < 1) Then Return SetError(0, 0, $sBinaryOrFilePath); Nothing to replace
    EndIf
    If (StringLeft($sBinary, 2) = "0x") Then $sBinary = StringTrimLeft($sBinary, 2)
    If (StringLen($sSearch) > 1) And (Mod(StringLen($sBinary), 2) = 0) And (Mod(StringLen($sSearch), 2) = 0) And (Mod(StringLen($sReplace), 2) = 0) Then
        $bSplit = StringRegExp($sBinary, "(?s).{1,2}", 3)
        $sBinary = ""
        For $i = 0 To UBound($bSplit) - 1
            $sBinary &= $bSplit[$i] & " "
        Next
        If (StringLeft($sBinary, 1) = " ") Then $sBinary = StringTrimLeft($sBinary, 1)
        If (StringLeft($sSearch, 2) = "0x") Then $sSearch = StringTrimLeft($sSearch, 2)
        $bSplit = StringRegExp($sSearch, "(?s).{1,2}", 3)
        $sSearch = ""
        For $i = 0 To UBound($bSplit) - 1
            $sSearch &= $bSplit[$i] & " "
        Next
        If (StringLeft($sSearch, 1) = " ") Then $sSearch = StringTrimLeft($sSearch, 1)
        If (StringLeft($sReplace, 2) = "0x") Then $sReplace = StringTrimLeft($sReplace, 2)
        If $sReplace <> "" Then
            $bSplit = StringRegExp($sReplace, "(?s).{1,2}", 3)
            $sReplace = ""
            For $i = 0 To UBound($bSplit) - 1
                $sReplace &= $bSplit[$i] & " "
            Next
            If (StringLeft($sReplace, 1) = " ") Then $sReplace = StringTrimLeft($sReplace, 1)
        EndIf
    Else
        ConsoleWrite("! Hex incorrect:  may cause incorrect results." & @CRLF)
    EndIf
    $sBinary = StringReplace($sBinary, $sSearch, $sReplace, $sOccurrence, 1)
    $sExtended = @extended
    $sBinary = "0x" & StringReplace($sBinary, " ", "")
    If ($sBinary = "0x") Then $sBinary = ""
    If $sReturn Then Return SetError(0, $sExtended, $sBinary);Rerun result
    If $sExtended < 1 Then
        If (StringLeft($sBinaryOrFilePath, 2) = "0x") Then Return SetError(0, 0, $sBinaryOrFilePath); Nothing to replace
        Return SetError(0, 0, 0); Nothing to replace
    EndIf
    $hOpen = FileOpen($sBinaryOrFilePath, 2 + 16)
    If @error Then Return SetError(6, 0, 0) ; Open file write eror
    $sReturn = FileWrite($hOpen, Binary($sBinary))
    If $sReturn <> 0 Then Return SetError(0, $sExtended, 1);Rerun result write
    Return SetError(7, $sExtended, 0) ; write file eror
EndFunc   ;==>_BinaryReplace
Edited by Trong
optimize 1

Regards,
 

Share this post


Link to post
Share on other sites

I think you dont need to use so much regexp, loop and checking...

Local $sString="0x200000200100200200200300200400200500200600200700200800200900201000202000203000204000205000206000207000208000209000200A00200B00200C00200D00200E00200F0020A00020B00020C00020D00020E00020F000"
ConsoleWrite("+ IN: " & $sString & @CRLF)
$sString=BinaryToString($sString)
$sString=StringReplace($sString,Chr(0x00),"")
ConsoleWrite("!OUT: " & StringToBinary($sString) & @CRLF & @CRLF)

Saludos

Share this post


Link to post
Share on other sites

I think you dont need to use so much regexp, loop and checking...

Local $sString="0x200000200100200200200300200400200500200600200700200800200900201000202000203000204000205000206000207000208000209000200A00200B00200C00200D00200E00200F0020A00020B00020C00020D00020E00020F000"
ConsoleWrite("+ IN: " & $sString & @CRLF)
$sString=BinaryToString($sString)
$sString=StringReplace($sString,Chr(0x00),"")
ConsoleWrite("!OUT: " & StringToBinary($sString) & @CRLF & @CRLF)

Saludos

1 person likes this

Share this post


Link to post
Share on other sites

How to optimize it, please let me know!
I tried to make it work correctly, and less possible errors!


Regards,
 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Func BinaryReplace($bBinary, $bSearch, $bReplace, $iOccurence = 0)
    Local $sString = StringReplace(BinaryToString($bBinary), BinaryToString($bSearch), BinaryToString($bReplace), $iOccurence, 1)
    Return SetError (@error, @extended, Binary($sString))
EndFunc
$bBinary="0x200000200100200200200300200400200500200600200700200800200900201000202000203000204000205000206000207000208000209000200A00200B00200C00200D00200E00200F0020A00020B00020C00020D00020E00020F000"
$bSearch = "0x00"
$bReplace = ""
$bBinary2 = BinaryReplace($bBinary, $bSearch, $bReplace)

ConsoleWrite($bBinary & @CRLF)
ConsoleWrite($bBinary2 & @CRLF)

 

Edited by trancexx
Added example
1 person likes this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

If you want to replace some binary inside a file then use that function to write another function. Like this maybe:

Func FileBinaryReplace($sFile, $bSearch, $bReplace, $iOccurence = 0)
    Local $hFile = FileOpen($sFile, 17) ; binary mode, read, write
    FileSetPos($hFile, 0, 0)
    Local $bBinary = FileRead($hFile)
    FileSetPos($hFile, 0, 0)
    FileWrite($hFile, BinaryReplace($bBinary, $bSearch, $bReplace, $iOccurence))
    FileClose($hFile)
EndFunc

Add some error checking and you are done.

1 person likes this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Func BinaryReplace($bBinary, $bSearch, $bReplace, $iOccurence = 0)
    Local $sString = StringReplace(BinaryToString($bBinary), BinaryToString($bSearch), BinaryToString($bReplace), $iOccurence, 1)
    Return SetError (@error, @extended, Binary($sString))
EndFunc
$bBinary="0x200000200100200200200300200400200500200600200700200800200900201000202000203000204000205000206000207000208000209000200A00200B00200C00200D00200E00200F0020A00020B00020C00020D00020E00020F000"
$bSearch = "0x00"
$bReplace = ""
$bBinary2 = BinaryReplace($bBinary, $bSearch, $bReplace)

ConsoleWrite($bBinary & @CRLF)
ConsoleWrite($bBinary2 & @CRLF)

 

If you want to replace some binary inside a file then use that function to write another function. Like this maybe:

Func FileBinaryReplace($sFile, $bSearch, $bReplace, $iOccurence = 0)
    Local $hFile = FileOpen($sFile, 17) ; binary mode, read, write
    FileSetPos($hFile, 0, 0)
    Local $bBinary = FileRead($hFile)
    FileSetPos($hFile, 0, 0)
    FileWrite($hFile, BinaryReplace($bBinary, $bSearch, $bReplace, $iOccurence))
    FileClose($hFile)
EndFunc

Add some error checking and you are done.

 

 

Please check your script, it does not work correctly!

I try with my function it works!

 

;# TEST 1

Global $TestFile = @DesktopDir & "\Test.txt"
Global $TestData= "0x00010203040506070811121415161718192131"
;# Start write Test file!
$hFileOpen = FileOpen($TestFile, 2 + 16)
FileWrite($hFileOpen, Binary($TestData))
FileClose($hFileOpen)
;# End write Test file!

Local $hFileOpen = FileOpen($TestFile, 16)
Global $sFileContent = FileRead($hFileOpen)
FileClose($hFileOpen)
Local $sListBadString = "00|01|02|03|04|05|06|07|08|11|12|14|15|16|17|18|19"
Local $sBadString = StringSplit($sListBadString, "|")
For $s = 1 To UBound($sBadString) - 1
    $sFileContent = BinaryReplace($sFileContent, $sBadString[$s], "")
;~  $sFileContent = _BinaryReplace($sFileContent,  $sBadString[$s], "")
Next

$hFileOpen = FileOpen($TestFile, 2 + 16)
FileWrite($hFileOpen, Binary($sFileContent))
FileClose($hFileOpen)
; Display contents of file.
ConsoleWrite(FileRead($TestFile) & @LF)


;# TEST 2

;# Start write Test file!
$hFileOpen = FileOpen($TestFile, 2 + 16)
FileWrite($hFileOpen, Binary($TestData))
FileClose($hFileOpen)
;# End write Test file!

For $s = 1 To UBound($sBadString) - 1
    FileBinaryReplace($TestFile, $sBadString[$s], "")
;~  _BinaryReplace($TestFile, $sBadString[$s], "")
Next

; Display contents of file.
ConsoleWrite(FileRead($TestFile) & @LF)

Func FileBinaryReplace($sFile, $bSearch, $bReplace, $iOccurence = 0)
    Local $hFile = FileOpen($sFile, 17) ; binary mode, read, write
    FileSetPos($hFile, 0, 0)
    Local $bBinary = FileRead($hFile)
    FileSetPos($hFile, 0, 0)
    FileWrite($hFile, BinaryReplace($bBinary, $bSearch, $bReplace, $iOccurence))
    FileClose($hFile)
EndFunc   ;==>FileBinaryReplace

Func BinaryReplace($bBinary, $bSearch, $bReplace, $iOccurence = 0)
    Local $sString = StringReplace(BinaryToString($bBinary), BinaryToString($bSearch), BinaryToString($bReplace), $iOccurence, 1)
    Return SetError(@error, @extended, Binary($sString))
EndFunc   ;==>BinaryReplace


Func _BinaryReplace($sBinaryOrFilePath, $sSearch, $sReplace, $sOccurrence = 0)
    If $sOccurrence = Default Or $sOccurrence = -1 Then $sOccurrence = 0
    Local $sBinary = $sBinaryOrFilePath
    Local $sReturn = 1
    Local $hOpen, $bSplit, $i, $sExtended
    If FileExists($sBinaryOrFilePath) Then
        $hOpen = FileOpen($sBinaryOrFilePath, 16)
        If @error Then Return SetError(2, 0, "");Open file error
        $sBinary = FileRead($hOpen)
        If @error Then Return SetError(3, 0, ""); Read file error
        FileClose($hOpen)
        $sReturn = 0
    Else
        If (StringLen($sBinaryOrFilePath) < 1) Or (StringLeft($sBinaryOrFilePath, 2) <> "0x") Or (StringLen($sSearch) < 1) Then Return SetError(0, 0, $sBinaryOrFilePath); Nothing to replace
    EndIf
    If (StringLeft($sBinary, 2) = "0x") Then $sBinary = StringTrimLeft($sBinary, 2)
    If (StringLen($sSearch) > 1) And (Mod(StringLen($sBinary), 2) = 0) And (Mod(StringLen($sSearch), 2) = 0) And (Mod(StringLen($sReplace), 2) = 0) Then
        $bSplit = StringRegExp($sBinary, "(?s).{1,2}", 3)
        $sBinary = ""
        For $i = 0 To UBound($bSplit) - 1
            $sBinary &= $bSplit[$i] & " "
        Next
        If (StringLeft($sBinary, 1) = " ") Then $sBinary = StringTrimLeft($sBinary, 1)
        If (StringLeft($sSearch, 2) = "0x") Then $sSearch = StringTrimLeft($sSearch, 2)
        $bSplit = StringRegExp($sSearch, "(?s).{1,2}", 3)
        $sSearch = ""
        For $i = 0 To UBound($bSplit) - 1
            $sSearch &= $bSplit[$i] & " "
        Next
        If (StringLeft($sSearch, 1) = " ") Then $sSearch = StringTrimLeft($sSearch, 1)
        If (StringLeft($sReplace, 2) = "0x") Then $sReplace = StringTrimLeft($sReplace, 2)
        If $sReplace <> "" Then
            $bSplit = StringRegExp($sReplace, "(?s).{1,2}", 3)
            $sReplace = ""
            For $i = 0 To UBound($bSplit) - 1
                $sReplace &= $bSplit[$i] & " "
            Next
            If (StringLeft($sReplace, 1) = " ") Then $sReplace = StringTrimLeft($sReplace, 1)
        EndIf
    Else
        ConsoleWrite("! Hex incorrect:  may cause incorrect results." & @CRLF)
    EndIf
    $sBinary = StringReplace($sBinary, $sSearch, $sReplace, $sOccurrence, 1)
    $sExtended = @extended
    $sBinary = "0x" & StringReplace($sBinary, " ", "")
    If ($sBinary = "0x") Then $sBinary = ""
    If $sReturn Then Return SetError(0, $sExtended, $sBinary);Rerun result
    If $sExtended < 1 Then
        If (StringLeft($sBinaryOrFilePath, 2) = "0x") Then Return SetError(0, 0, $sBinaryOrFilePath); Nothing to replace
        Return SetError(0, 0, 0); Nothing to replace
    EndIf
    $hOpen = FileOpen($sBinaryOrFilePath, 2 + 16)
    If @error Then Return SetError(6, 0, 0) ; Open file write eror
    $sReturn = FileWrite($hOpen, Binary($sBinary))
    If $sReturn <> 0 Then Return SetError(0, $sExtended, 1);Rerun result write
    Return SetError(7, $sExtended, 0) ; write file eror
EndFunc   ;==>_BinaryReplace

 

Or  I wrong somewhere?


Regards,
 

Share this post


Link to post
Share on other sites

^^ Add "0x".

$sFileContent = BinaryReplace($sFileContent, "0x" & $sBadString[$s], "")
;...
FileBinaryReplace($TestFile, "0x" & $sBadString[$s], "")

 

 


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

^^ Add "0x".

$sFileContent = BinaryReplace($sFileContent, "0x" & $sBadString[$s], "")
;...
FileBinaryReplace($TestFile, "0x" & $sBadString[$s], "")

 

 

 

Not working:
 

FileBinaryReplace($TestFile, "0x" &$sBadString[$s], "")

 

I changed a few things,and it worked!

Func BinaryReplace($bBinary, $bSearch, $bReplace, $iOccurence = 0)
    If (StringLeft($bSearch, 2) <> "0x") And ($bSearch <> "") Then $bSearch = "0x" & $bSearch
    If (StringLeft($bReplace, 2) <> "0x") And ($bReplace <> "") Then $bReplace = "0x" & $bReplace
    Local $sString = StringReplace(BinaryToString($bBinary), BinaryToString($bSearch), BinaryToString($bReplace), $iOccurence, 1)
    Return SetError(@error, @extended, Binary($sString))
EndFunc   ;==>BinaryReplace

Func FileBinaryReplace($sFile, $bSearch, $bReplace, $iOccurence = 0)
    Local $hFileOpen = FileOpen($sFile, 16)
    Local $sFileContent = FileRead($hFileOpen)
    FileClose($hFileOpen)
    $hFileOpen = FileOpen($TestFile, 2 + 16)
    FileWrite($hFileOpen, Binary(BinaryReplace($sFileContent, $bSearch, $bReplace, $iOccurence)))
    FileClose($hFileOpen)
EndFunc   ;==>FileBinaryReplace

 

But it only supports standard characters!

 

Global $TestData = "0x00010203040506070811121415161718192131010"

ConsoleWrite( BinaryReplace($TestData, "010" , "888")& @CRLF)
;~ ConsoleWrite( _BinaryReplace($TestData, "010" , "888")& @CRLF)

 

Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites

Standard how? "010" in binary is not something that's obvious. Do you want to replace binary data or something that you think it's binary data?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

^^ You're right!
But I tried to create something like the hex patcher! :lol:

Edited by Trong
Just the hex, not binary data (10101010)!

Regards,
 

Share this post


Link to post
Share on other sites

The smallest addressable unit of binary data is 1 byte. That's for example "0x10". "Hex patchers" work with that.

"0x010" is something else.

 


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

It's like "0x010?" but working for "0x010"! :P



Although there 3GB RAM but trouble appeared when working with large files over 40MB! :mad:

A_error.png

Has 4:00 AM, I need to sleep! :yawn::yawn:
Will research this problem later!

Edited by Trong

Regards,
 

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