carl1905 Posted August 21, 2016 Posted August 21, 2016 This code will read text data from txt file. However, if I use '$Newtext &= $convert' then $convert is saved as strings, not hex value. For example, if $convert = 0x300020FF and $bNewText = Test then the result is 0x333030303230464654657374. (Wrong) What I want is 0x300020FF54657374. How can I modify my script? For $i = $y-1 To Ubound($split) - 1 $convert = "0x" & $split[$i][0] $Newtext &= $convert $NewLen_hex = BinaryLen ($convert) $i=$i+1 $split[$i][1] = $split[$i][0] $split[$i][1] = StringRegExpReplace($split[$i][1], "<cf>", @CRLF) $split[$i][1] = StringRegExpReplace($split[$i][1], "<lf>", @LF) $split[$i][1] = StringRegExpReplace($split[$i][1], "<cr>", @CR) $bNewText = _WinAPI_WideCharToMultiByte($split[$i][1], $CP_SHIFT_JIS) if $i = Ubound($split) - 1 Then $bNewText &= Chr(0) EndIf $Newtext &= $bNewText $NewLen = $NewLen_hex + BinaryLen ($bNewText) Next (...) (...) $Newfile = BinaryMid($Newfile_header,1) & BinaryMid($Newtext,1) $hNewfile = FileOpen ("NEW_"&$Name, 2+16) FileWrite ($hNewfile, $Newfile) FileClose ($hNewfile) TrayTip ("Importer", "Finish!", 3) sleep (3000)
Developers Jos Posted August 21, 2016 Developers Posted August 21, 2016 It would help when you show a snippet that shows the issue and we can run. Jos 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.
carl1905 Posted August 21, 2016 Author Posted August 21, 2016 (edited) 8 minutes ago, Jos said: It would help when you show a snippet that shows the issue and we can run. Jos Ok. My code makes a new binary file from original file and txt. However, I'm major in atomic physics and I'm not sure how can I show you my script as a snippet, so I just post my whole code and sample file. expandcollapse popup#include <File.au3> #include <Winapi.au3> #include <Binary.au3> #include <String.au3> #include <Array.au3> #include <MsgBoxConstants.au3> #include <FileConstants.au3> Global Const $CP_SHIFT_JIS = 932 Func _ArrayAddColumns(ByRef $aArr, $iNumColToAdd = 1) If IsArray($aArr) = 0 Then Return SetError(1, 0, -1) ; Filter out non-array If $iNumColToAdd < 1 Then Return SetError(2, 0, -1) ; $iNumColToAdd must be greater than zero to add a column. If UBound($aArr, 0) > 2 Then Return SetError(3, 0, -1) ; Only allows a 1d or 2d array pass this line. If UBound($aArr, 0) = 1 Then ; ====== For 1d array ======== Local $aRet[UBound($aArr)][$iNumColToAdd + 1] ; Create new 2d array. For $r = 0 To UBound($aArr) - 1 $aRet[$r][0] = $aArr[$r] Next Else ; ======= For 2d array ============ Local $aRet = $aArr ; So that ByRef $aArr is not altered outside of function. ReDim $aRet[UBound($aRet)][UBound($aRet, 2) + $iNumColToAdd] ; ReDim 2d array only. EndIf Return $aRet EndFunc ;==>_ArrayAddColumns Dim $NEWdata $TxtPath = FileOpenDialog("Select the TXT file", @ScriptDir, "text files (*.txt)",1) If @error = 1 Then Exit _FileReadToArray($TxtPath,$NEWdata) $Name = StringTrimRight(CompGetFileName($TxtPath),4) $File = FileOpen ($Name, 0+16) If $File = -1 Then MsgBox(0,"Error!","Can't open "&$Name&" file.") Exit EndIf FileSetPos($File,4,0) $table_off = Dec(Hex(FileRead($File,2))) ; 0x1E2C Read $Base = $table_off * 2 ; 0x3C58 FileSetPos($File,$Base,0) ; 0x3C58 $table_size = Dec(Hex(FileRead($File,4))) ; 0x300. From 0x3C58 + 0x300 = 0x3F58 $table_end = $Base + $table_size ; table_end is exactly same as starting point of string tbl. $Offset = $table_end ; let table_end as offset FileSetPos($File,0,0) $Newfile_header = FileRead($File,$table_end) ; read *.FPS4 from 0 to offset = Making header file FileSetPos($File,$Base,0) $Newtext = "" $Newfile = "" $j = 1 Dim $Text Do $pos = FileGetPos($File) $split = StringSplit($NEWdata[$j],"{}") $split = _ArrayAddColumns($split) ;_ArrayDisplay($split) if StringInStr($NEWdata[$j], "{") = 1 Then $y = 3 For $i = $y-1 To Ubound($split) - 1 $convert = "0x" & $split[$i][0] $Newtext &= $convert $NewLen_hex = BinaryLen ($convert) $i=$i+1 $split[$i][1] = $split[$i][0] $split[$i][1] = StringRegExpReplace($split[$i][1], "<cf>", @CRLF) $split[$i][1] = StringRegExpReplace($split[$i][1], "<lf>", @LF) $split[$i][1] = StringRegExpReplace($split[$i][1], "<cr>", @CR) $bNewText = _WinAPI_WideCharToMultiByte($split[$i][1], $CP_SHIFT_JIS) if $i = Ubound($split) - 1 Then $bNewText &= Chr(0) EndIf $Newtext &= $bNewText $NewLen = $NewLen_hex + BinaryLen ($bNewText) Next Else $y = 1 For $i = $y-1 To Ubound($split) - 1 if $split[$i][0] = 1 Then $i=1 $split[$i][0] = StringRegExpReplace($split[$i][0], "<cf>", @CRLF) $split[$i][0] = StringRegExpReplace($split[$i][0], "<lf>", @LF) $split[$i][0] = StringRegExpReplace($split[$i][0], "<cr>", @CR) $bNewText = _WinAPI_WideCharToMultiByte($split[$i][0], $CP_SHIFT_JIS) & Chr(0) $Newtext &= $bNewText $NewLen = BinaryLen ($bNewText) Else For $i = $y - 1 To Ubound($split) - 1 if $i <> 0 Then $convert = "0x" & $split[$i][0] $Newtext &= $convert $NewLen_hex = BinaryLen ($convert) EndIf $i=$i+1 $split[$i][1] = $split[$i][0] $split[$i][1] = StringRegExpReplace($split[$i][1], "<cf>", @CRLF) $split[$i][1] = StringRegExpReplace($split[$i][1], "<lf>", @LF) $split[$i][1] = StringRegExpReplace($split[$i][1], "<cr>", @CR) $bNewText = _WinAPI_WideCharToMultiByte($split[$i][1], $CP_SHIFT_JIS) if $i = Ubound($split) - 1 Then $bNewText &= Chr(0) $Newtext &= $bNewText $NewLen = $NewLen_hex + BinaryLen ($bNewText) EndIf Next EndIf Next EndIf $New_Offset = _BinaryReverse($table_size + $Offset - $table_end) $Newfile_header = _BinaryPoke($Newfile_header,$pos+1,$New_Offset,"dword") $Offset += $NewLen $pos = $pos + 4 FileSetPos($File,$pos,0) $j += 1 $convert = "" $bNewText = "" Until $pos = $table_end $Newfile = BinaryMid($Newfile_header,1) & BinaryMid($Newtext,1) $hNewfile = FileOpen ("NEW_"&$Name, 2+16) FileWrite ($hNewfile, $Newfile) FileClose ($hNewfile) TrayTip ("Importer", "Finish!", 3) sleep (3000) Func __HexToString($sString) If StringLeft($sString, 2) <> "0x" Then $sString = "0x" & $sString Return BinaryToString($sString) EndFunc Func CompGetFileName($Path) If StringLen($Path) < 4 Then Return -1 $ret = StringSplit($Path,"\",2) If IsArray($ret) Then Return $ret[UBound($ret)-1] EndIf If @error Then Return -1 EndFunc sample.zip Edited August 21, 2016 by carl1905
carl1905 Posted August 21, 2016 Author Posted August 21, 2016 I found that only write mode saves "1F010006300020FF" as 0x1F010006300020FF in binary file. However, my script use filewrite at the end of code. I don't have idea how to write "1F010006300020FF" in advance and then &= $Newtext.
Developers Jos Posted August 22, 2016 Developers Posted August 22, 2016 Still think you should minimize your script to a few lines that simply show the issue you have. Shouldn't be that hard. Jos 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.
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