Jump to content

_FileReadBin


blindwig
 Share

Recommended Posts

I needed to read some binary data out of a file, so I wrote this function. It takes a file handle (you must have already opened the file in read mode) and reads the specified number of bytes. In returns the value of each byte in an array.

CODE

Func _FileReadBin($hndFile, $Amount = 1)

If $Amount <= 0 or not StringIsDigit($hndFile) Then

SetError(1)

Return 0

EndIf

Dim $aReturn[$Amount + 1], $num = 0, $tmp = ''

For $Num = 1 to $Amount

$tmp = FileRead($hndFile, 1)

If @error Then ExitLoop

$aReturn[$num] = Asc($tmp)

Next

ReDim $aReturn[$num]

$aReturn[0] = $num - 1

If $aReturn[0] < $Amount Then SetError(1)

Return $aReturn

EndFunc

Link to comment
Share on other sites

I needed to read some binary data out of a file, so I wrote this function.  It takes a file handle (you must have already opened the file in read mode) and reads the specified number of bytes.  In returns the value of each byte in an array.

CODE

Func _FileReadBin($hndFile, $Amount = 1)

If $Amount <= 0 or not StringIsDigit($hndFile) Then

  SetError(1)

  Return 0

EndIf

Dim $aReturn[$Amount + 1], $num = 0, $tmp = ''

For $Num = 1 to $Amount

  $tmp = FileRead($hndFile, 1)

  If @error Then ExitLoop

  $aReturn[$num] = Asc($tmp)

Next

ReDim $aReturn[$num]

$aReturn[0] = $num - 1

If $aReturn[0] < $Amount Then SetError(1)

Return $aReturn

EndFunc

<{POST_SNAPBACK}>

Will this work on files that contain nulls??

If so you just solved a problem people have been try to solve since the arrival of Autoit3

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
Link to comment
Share on other sites

Will this work on files that contain nulls??

If so you just solved a problem people have been try to solve since the arrival of Autoit3

<{POST_SNAPBACK}>

Yes, it reads nulls. Since it does not store the actual character, only the value, then a null is just a zero, and AutoIt is perfectly capable of storing zeros :(

It might not be the fastest routine out there, but it does the job...

Link to comment
Share on other sites

Yes, it reads nulls.  Since it does not store the actual character, only the value, then a null is just a zero, and AutoIt is perfectly capable of storing zeros :(

It might not be the fastest routine out there, but it does the job...

<{POST_SNAPBACK}>

Awesome! :(

Great job.

i can use this for a lot of programs and now we can compare binary files natively in Autoit.

Edited by SolidSnake
HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
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...