Jump to content

How write bytes into a file one by one?


Recommended Posts

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
Link to comment
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 :)

Link to comment
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)
Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

  • 8 years later...

#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

  • Developers

@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

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