Jump to content

Read file and convert to hex like in editor ?


Recommended Posts

I am trying to read in a file and have autoit write it back out as a hex file. I need the hex file that is written out to exactly match that of how a hex editor like hex workshop would save the file if I loaded it and saved it as hex from that program.

I can get autoit to write the file as hex, but it mangles some of the values, so if I had FF 24 times in a row in what hex workshop would output, my text file from autoit has FF 32 times.

$FileToOpen = "data.bin"

$Filebin = FileOpen ($FileToOpen,4) ; Read file with bin extension into $Filebin

$size = FileGetSize($FileToOpen)

For $a = 1 to $size

$chars = FileRead($Filebin,1)

$one = $one & hex( $chars)

Next

FileClose($Filebin)

$Filebin = FileOpen("data.txt", 2)

FileWrite($Filebin, $one)

FileClose($Filebin)

exit

Link to comment
Share on other sites

you need to open the file in binary mode: 16

Thanks for the tip !

The problem now is that it returns the characters prefixed with 0x, so FF become 0xFF

If I use something like StringTrimLeft to remove the first two characters that gives me the desired result, but it is too slow for large files and I have to read it 1 byte at a time..

Any ideas ?

Thanks in advance.

Link to comment
Share on other sites

$FileToOpen = "data.bin"
$Filebin = FileOpen ($FileToOpen,16) ; Read file with bin extension into $Filebin
$size = FileGetSize($FileToOpen)

$one = ""

For $a = 1 to $size
  $chars = FileRead($Filebin,1)
  $one = $one & hex( $chars)
Next

FileClose($Filebin)
$Filebin = FileOpen("data.txt", 2)


FileWrite($Filebin, $one)
FileClose($Filebin)
exit

This works fine for me :D

For my random example it returns :

EBFEEBFE8F98A9A0F0F09098436FFFFF909F0FFF

Link to comment
Share on other sites

Thanks for the tip !

The problem now is that it returns the characters prefixed with 0x, so FF become 0xFF

If I use something like StringTrimLeft to remove the first two characters that gives me the desired result, but it is too slow for large files and I have to read it 1 byte at a time..

Any ideas ?

Thanks in advance.

Look at _WinAPI_SetFilePointer() + _WinAPI_ReadFile()

http://www.autoitscript.com/forum/index.php?showtopic=76829

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