yookee Posted July 13, 2010 Share Posted July 13, 2010 (edited) I want write data(hex) into a file, one byte at a time.ButFileWrite($outfile, Binary($var)) ;should write 4 bytes onceFileWrite($outfile, Chr($var)) ;when $var>128, should write 3F, looks like ? as textExample:$outfile = FileOpen("out.dat", 18) $var=129 FileWrite($outfile, Binary($var)) FileWrite($outfile, Chr($var)) FileClose($outfile)Open out.dat as HEXoffset(d) 00 01 02 03 0500000000 81 00 00 00 3F ....?How can I get a file as follow?offset(d) 0000000000 81 Edited July 13, 2010 by yookee Link to comment Share on other sites More sharing options...
enaiman Posted July 13, 2010 Share Posted July 13, 2010 Did you actually check what Binary and Chr are doing before writing this:FileWrite($outfile, Binary($var)) ;should write 4 bytes onceFileWrite($outfile, Chr($var)) ;when $var>128, should write 3F, looks like ? as textLast time when I checked, the ASCII code for "?" was 63And Binary(129) is exactly 0x81000000 SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
AndyG Posted July 13, 2010 Share Posted July 13, 2010 -open file -set filepointer to byte -write byte Link to comment Share on other sites More sharing options...
Malkey Posted July 13, 2010 Share Posted July 13, 2010 This might help. #include <Constants.au3> ; Has $FILE_BEGIN define. Local $var = 129 Local $str = "char var1" Local $a = DllStructCreate($str) DllStructSetData($a, "var1", 0x67) ;===== Write to file ====== Local $outfile = FileOpen("out.dat", 18) ;FileWrite($outfile, Binary($var)) FileWrite($outfile, Chr(0x81)) FileWrite($outfile, Chr($var)) FileWrite($outfile, Chr(0x00)) FileWrite($outfile, Chr(0x3F)) FileWrite($outfile, DllStructGetData($a, 1)) FileSetPos($outfile, 2, $FILE_BEGIN); Resets file position. FileWrite($outfile, Chr(0x01)) ; This replaces 3rd byte "00" with "01". FileClose($outfile) ;===== Read file ======== Local $file = FileOpen("out.dat", 16) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ConsoleWrite(FileRead($file) & @CRLF) ; Output is 0x8181013F67 FileClose($file) Link to comment Share on other sites More sharing options...
yookee Posted July 14, 2010 Author Share Posted July 14, 2010 Did you actually check what Binary and Chr are doing before writing this:Last time when I checked, the ASCII code for "?" was 63And Binary(129) is exactly 0x81000000Did you view my question carefully? That just is my question that FileWrite($outfile, Chr($var)) will export 3F as Hex for every $var more than 128 in my environment.In other words, FileWrite($outfile, Chr(0x80)) output 0x80FileWrite($outfile, Chr(0x81)) output 0x3FFileWrite($outfile, Chr(0x82)) output 0x3F...and so on. Link to comment Share on other sites More sharing options...
yookee Posted July 14, 2010 Author Share Posted July 14, 2010 -open file-set filepointer to byte-write bytePlease, in my code$outfile = FileOpen("out.dat", 18)18=16+2binary mode and overwriteDo you mean filepointer is $var? There is not Byte() in AutoIt3, just Binary(), where it is 32bit.I want to write 8bit at a time. Link to comment Share on other sites More sharing options...
yookee Posted July 14, 2010 Author Share Posted July 14, 2010 This might help.Output is 0x3F3F013F67. Link to comment Share on other sites More sharing options...
Fill-up Posted November 20, 2018 Share Posted November 20, 2018 #include <StringConstants.au3> #include <WinAPIFiles.au3> $hlv = FileOpen("bin.bin", $FO_OVERWRITE + $FO_BINARY) For $b = 0 To 255 FileWrite($hlv, BinaryMid($b, 1, 1)) Next FileClose($hlv) This write a file 256 bytes long from 00 to ff one byte at a time. 00 01 02 03,,,,FD FE FF binary in normal write with 4 bytes. BinaryMid with 1, 1 sent the only first byte out. Link to comment Share on other sites More sharing options...
Developers Jos Posted November 21, 2018 Developers Share Posted November 21, 2018 @Fill-up, Welcome. One remark, he thread is over 8 years old and the OP hasn't been around for 6 years, so probably solved his issue already. 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. 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