Sign in to follow this  
Followers 0
yookee

How write bytes into a file one by one?

7 posts in this topic

#1 ·  Posted (edited)

I want write data(hex) into a file, one byte at a time.

But

FileWrite($outfile, Binary($var)) ;should write 4 bytes once

FileWrite($outfile, Chr($var)) ;when $var>128, should write 3F, looks like ? as text

Example:

$outfile = FileOpen("out.dat", 18)
$var=129
FileWrite($outfile, Binary($var))
FileWrite($outfile, Chr($var))
FileClose($outfile)

Open out.dat as HEX

offset(d) 00 01 02 03 05

00000000 81 00 00 00 3F ....?

How can I get a file as follow?

offset(d) 00

00000000 81

Edited by yookee

Share this post


Link to post
Share on other sites



Did you actually check what Binary and Chr are doing before writing this:

FileWrite($outfile, Binary($var)) ;should write 4 bytes once

FileWrite($outfile, Chr($var)) ;when $var>128, should write 3F, looks like ? as text

Last time when I checked, the ASCII code for "?" was 63

And 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 :)

Share this post


Link to post
Share on other sites

-open file

-set filepointer to byte

-write byte

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

Did you actually check what Binary and Chr are doing before writing this:

Last time when I checked, the ASCII code for "?" was 63

And Binary(129) is exactly 0x81000000

Did 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 0x80

FileWrite($outfile, Chr(0x81)) output 0x3F

FileWrite($outfile, Chr(0x82)) output 0x3F

...

and so on.

Share this post


Link to post
Share on other sites

-open file

-set filepointer to byte

-write byte

Please, in my code

$outfile = FileOpen("out.dat", 18)

18=16+2

binary mode and overwrite

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

Share this post


Link to post
Share on other sites

This might help.

Output is 0x3F3F013F67. :blink:

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
Sign in to follow this  
Followers 0