Jump to content

StringReplace wont work for binaries?


Recommended Posts

Hi,

Is there a way to do Hex / Binary editing now that StringReplace will no longer work on binaries?

The first code example worked on older versions of AutoIt before the Unicode? upgrade.

The second example wont overwrite the 3 digits in the file string stored in the variable $filedat

I have made the changes in the code for version 3.2.4.x

Currently running v3.2.4.9

;v3.2.x.x - This code would overwrite the 3 digits

Local $newg2 = 090, $oldg2 = 130

$File = @WorkingDir & "\CPDG520P.DAT" ; 12Kb binary EEPROM data file
$rdFile = FileOpen($File, 0) ; Open in READ mode
$FileDat = FileRead($rdFile) ; Read entire file to variable
$FileDat = StringReplace($FileDat, 9236, $newg2, 1, 0) ; replace 3 digit number at offset 9236 with 090
MsgBox(0, "# of Replacements", @extended)
FileClose($rdFile)

$rdFile = FileOpen($File, 2) ; Open in WRITE mode (erase previous contents)
MsgBox(0, "Writing to CPDG520P.DAT", "At Offset: 9236: " & $oldg2 & "  Replacing with:  " & $newg2)
FileWrite($rdFile, $FileDat)
FileClose($rdFile)

;-------------------------------------------------------

;v3.2.4.x (Using v3.2.4.9)  - This code wont overwrite the 3 digits and StringReplace @extended returns 0

Local $newg2 = 090, $oldg2 = 130

$File = @WorkingDir & "\CPDG520P.DAT" ; 12Kb binary EEPROM data file
$rdFile = FileOpen($File, 16) ; Open in Binary READ mode
$Filedat = FileRead($rdFile)  ; Read entire file to variable
$Filedat = BinaryToString($Filedat)
$FileDat = StringReplace($Filedat, 9236, $newG2, 1, 0) ; replace 3 digit number at offset 9236 with 090
MsgBox(0, "# of Replacements", @extended)
FileClose($rdFile)

$rdFile = FileOpen($File, 18) ; Open in WRITE mode (erase previous contents)
$Filedat = StringToBinary($Filedat)
FileWrite($rdFile, $Filedat)
FileClose($rdFile)

I see fascists...

Link to comment
Share on other sites

O.K. I finally got it to work, but with several problems.

@extended isn't returning replacement count from StringReplace, it always returns 0

though it is replacing the string (3 digits)

had problem with leading zero number eg. 070

StringReplace would only write 2 digits without leading zero

(using range of 050 to 180)

attempting to concatenate quotes didn't work eg, chr(148) or chr(34)

"chr(34) & $newg2 & chr(34)"

StringReplace with quotes around number with leading zero worked as it had in my original test code,

that's one thing i changed once i added the routine for getting the value from the slider,

before that i had hard coded the value (in quotes) in the StringReplace line for testing.

This works

; value in file 130
$FileDat = StringReplace($Filedat, 9236, '090', 1,0) ; replace 3 digit number at offset 9236 with 090
;result = 090
oÝ÷ Ù8b²åË
èµí­²Ú,¶¸§­­æx3zººÚ"µÍÈ[YH[[HLÌØØ[ ÌÍÛ]ÙÌHLÌÍÑ[Q]HÝ[ÔXÙJ    ÌÍÑ[Y]LÍ    ÌÍÛ]ÙÌK
BÜÝ[HLoÝ÷ Ù)⦭jëh×6
 ;need to add leading zero to slider number below 2 digits 

;$slideradd0 = 0 & GUICtrlRead($slider1) * -1 ; causes problems with StringReplace

If ControlGetFocus("New G2") = "msctls_trackbar321" Then
    If GUICtrlRead($Slider1) <> GUICtrlRead($input) Then
      If StringLen(GUICtrlRead($slider1) * -1) <> 3 Then
          $slideradd0 = chr(48) & GUICtrlRead($slider1) * -1 ; solution;  use chr(48) "0"
          GUICtrlSetData($input,$slideradd0)
      Else  
         GUICtrlSetData($input,GUICtrlRead($slider1) * -1)
          EndIf
    EndIf
Endif
;
;more code....
$newG2 = GUICtrlRead($input)
oÝ÷ Ùj+)àr^{¦¦WºÚ"µÍØØ[ ÌÍÛ]ÙÌHL   ÌÍÛÛÌHLÌÌÍÛ]ÙÌHÚ

H   [È ÌÍÛ]ÙÌÌÍÑ[HHÛÜÚ[Ñ   [È ][ÝÉÌLÐÔÍLU   ][ÝÈÈLØ[HQTÓH]H[BÈXY[HÈÝ[È[Y]ÌÍÜ[HH[[    ÌÍÑ[KMHÈÜ[[[HPQ[ÙBÌÍÑ[Y]H[TXY
    ÌÍÜ[JHÈXY[H[HÈXXBÌÍÑ[Y]H[UÔÝ[Ê   ÌÍÑ[Y]
BÌÍÛÛÌHÝ[ÓZY
    ÌÍÑ[Y]LÍÊNÈÙ]ÚXÝÈ]ÙÙ]LÍÌÍÑ[Q][ÔXÙJ ÌÍÑ[Y]LÍ    ÌÍÛ]ÑÌK
HÈXÙHÈYÚ][X]ÙÙ]LÍÚ]LÌÍÛ[H^[YÙÐÞ
    ][ÝÈÈÙXÙ[Y[É][ÝË    ÌÍÛ[
B[PÛÜÙJ  ÌÍÜ[JBÈÜ[[HÜÜ][ÂÌÍÜ[HH[[  ÌÍÑ[KN
HÈÜ[[ÔUH[ÙH
ÙH][ÝÈÛÛ[ÊBÌÍÑ[Y][ÕÐ[J    ÌÍÑ[Y]
B[]J ÌÍÜ[K    ÌÍÑ[Y]
B[PÛÜÙJ  ÌÍÜ[JBÈXY[H[Ù]Ú[ÙYÝ[È[YBÌÍÜ[HH[[ ÌÍÑ[KMHÈÜ[[[HPQ[ÙBÌÍÑ[Y]H[TXY
    ÌÍÜ[JHÈXY[H[HÈXXBÌÍÑ[Y]H[UÔÝ[Ê   ÌÍÑ[Y]
BÌÍÛ]ÙÌHÝ[ÓZY
    ÌÍÑ[Y]LÍÊNÈÙ]ÚXÝÈ]ÙÙ]LÍHÌ[PÛÜÙJ    ÌÍÜ[JBÈÜ^HYÜH[Y[YÂÙÐÞ
    ][ÝÉ][ÝË    ][ÝÉÌÍÛ]ÙÌ   ][ÝÈ  [È ÌÍÛ]ÙÌ [È ][ÝÈ  ÌÍÛÛÌ  ][ÝÈ  [È ÌÍÛÛÌB

I see fascists...

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...