Trong Posted January 3, 2016 Share Posted January 3, 2016 (edited) Local $sHexToReplace = "0x200000200100200200200300200400200500200600200700200800200900201000202000203000204000205000206000207000208000209000200A00200B00200C00200D00200E00200F0020A00020B00020C00020D00020E00020F000" ConsoleWrite("!> IN: " & $sHexToReplace & @CRLF) $sHexToReplace = StringReplace($sHexToReplace, "00", "") ConsoleWrite("!>OUT: " & $sHexToReplace & @CRLF) Edited January 12, 2016 by Trong Regards, Link to comment Share on other sites More sharing options...
mikell Posted January 3, 2016 Share Posted January 3, 2016 Your requirements are not clearly defined, we have to guess what you exactly want to doAssumption : 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) Link to comment Share on other sites More sharing options...
Trong Posted January 3, 2016 Author Share Posted January 3, 2016 Thank mikell very much!I replace the hex string (2 characters) in hex codeIf 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, Link to comment Share on other sites More sharing options...
Developers Jos Posted January 3, 2016 Developers Share Posted January 3, 2016 (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 January 3, 2016 by Jos Trong 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Trong Posted January 12, 2016 Author Share Posted January 12, 2016 (edited) expandcollapse popupLocal $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 January 12, 2016 by Trong optimize 1 Regards, Link to comment Share on other sites More sharing options...
Danyfirex Posted January 12, 2016 Share Posted January 12, 2016 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 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
Danyfirex Posted January 12, 2016 Share Posted January 12, 2016 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 Trong 1 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
Trong Posted January 12, 2016 Author Share Posted January 12, 2016 How to optimize it, please let me know!I tried to make it work correctly, and less possible errors! Regards, Link to comment Share on other sites More sharing options...
trancexx Posted January 12, 2016 Share Posted January 12, 2016 (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 January 12, 2016 by trancexx Added example Trong 1 ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
trancexx Posted January 12, 2016 Share Posted January 12, 2016 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) EndFuncAdd some error checking and you are done. Trong 1 ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Trong Posted January 12, 2016 Author Share Posted January 12, 2016 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) EndFuncAdd some error checking and you are done. Please check your script, it does not work correctly!I try with my function it works! expandcollapse popup;# 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, Link to comment Share on other sites More sharing options...
trancexx Posted January 12, 2016 Share Posted January 12, 2016 ^^ Add "0x".$sFileContent = BinaryReplace($sFileContent, "0x" & $sBadString[$s], "") ;... FileBinaryReplace($TestFile, "0x" & $sBadString[$s], "") ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Trong Posted January 12, 2016 Author Share Posted January 12, 2016 (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 January 12, 2016 by Trong Regards, Link to comment Share on other sites More sharing options...
trancexx Posted January 12, 2016 Share Posted January 12, 2016 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 Link to comment Share on other sites More sharing options...
Trong Posted January 12, 2016 Author Share Posted January 12, 2016 (edited) ^^ You're right!But I tried to create something like the hex patcher! Edited January 12, 2016 by Trong Just the hex, not binary data (10101010)! Regards, Link to comment Share on other sites More sharing options...
trancexx Posted January 12, 2016 Share Posted January 12, 2016 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 Link to comment Share on other sites More sharing options...
Trong Posted January 12, 2016 Author Share Posted January 12, 2016 (edited) It's like "0x010?" but working for "0x010"! Although there 3GB RAM but trouble appeared when working with large files over 40MB! Has 4:00 AM, I need to sleep! Will research this problem later! Edited January 12, 2016 by Trong Regards, Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now