Jump to content

Writing hex into a file


Skrip
 Share

Recommended Posts

How can you write to the first line inside...say... a picture file? Like so if you open it in a hex editor, it will show the text on the first line.

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

How can you write to the first line inside...say... a picture file? Like so if you open it in a hex editor, it will show the text on the first line.

You would just write it in binary mode, but the change may cause the rest of the file format to be unrecognized. So you would have to know what can be changed and still preserve normal operation of the file.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Binary files don't exactly have "lines". And all hex editors are different in the length of the "lines" that are displayed. Some will display the data in lines of 16 bytes, and others will display it in lines of 8 bytes.

I'm not sure how you would actually write a specifically sized value to a binary file in AutoIt, but to write a value (of unknown size?) you would do something like this:

$hFile = FileOpen("test.bin", BitOR(16, 1))
FileWrite($hFile, Binary(547)) ;should write 4 bytes
FileClose($hFile)
Edited by cppman
Link to comment
Share on other sites

How do I know the value for like..letters?

Er...would it be something like this?

$bin = StringToBinary("Test")
$hFile = FileOpen("test.bin", BitOR(16, 1)); Okay. What exactly is the BitOR for? I think I know, but not sure.
FileWrite($hFile, $bin);should write 4 bytes
FileClose($hFile)

EDIT: Yes..This worked perfectly actually. Now how exactly could I write it to the top of the file, and not at the end?

Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

BitOR(16, 1)

is combining the "Binary" and "Write" flags, which tells AutoIt how to open the file. Its basically saying, I want to write to this binary file.

As for the value of letters, you simply get the ASCII (or Unicode) value (that is what StringToBinary does). The size of the file will be:

size of character * length of string

So, if you are using unicode, and each character is 2 bytes, what you just wrote to the file would be 8 bytes. If you used ASCII, the size of the file would be 4 bytes.

Link to comment
Share on other sites

Okay. I understand. The examples above seem to work perfectly. Now how exactly could I write it to the top of the file, and not at the end?

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

Look at _WinAPI_CreateFile, and _WinAPI_SetFilePointer. _WinAPI_SetFilePointer isn't part of the WinAPI lib in AutoIt, so here it is below:

Func _WinAPI_SetFilePointer($hFile, $nDistance, $nMethod = 0)
    $nRet = DllCall("kernel32.dll", "dword", "SetFilePointer", "ptr", $hFile, "long", $nDistance, "ptr", 0, "dword", $nMethod)
    return $nRet[0]
EndFunc

Basically, just open the file with the fist function, then set the position you would like to start writing to with the second function.

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