Sign in to follow this  
Followers 0
richietheprogrammer

hex search, replace

13 posts in this topic

Hi everyone, I am trying to search for text in a bin file (hex), and I am trying to replace the text (all without actually opening the file).

So I am guessing I have to fileopen it in hex. I also found filesetpos, which doesnt really help me because I am not sure if the offset is consistent. So i want to search, and replace in hex. Any help is much appreciated :unsure:

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

;File test.au3
$fh = FileOpen("test.au3", 16)
$SH = FileRead($fh)
FileClose($fh)
$SH = StringReplace($SH, "246668", "000000")
$fh = FileOpen("test2.au3",  17)
FileWrite($fh, $SH)
FileClose($fh)

This will replace "$fh" with "NUL NUL NUL".

But you can't replace text in a file without opening it. Doesn't make sense to me... :unsure:

Edited by Hannes123

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

;File test.au3
$fh = FileOpen("test.au3", 16)
$SH = FileRead($fh)
FileClose($fh)
$SH = StringReplace($SH, "246668", "000000")
$fh = FileOpen("test2.au3",  17)
FileWrite($fh, $SH)
FileClose($fh)

This will replace "$fh" with "NUL NUL NUL".

But you can't replace text in a file without opening it. Doesn't make sense to me... :unsure:

Hmm thanks for this. What I meant by open it is I dont want to physically open it, and hit ctrl f..... What you provided looks like what I wanted. But, the file has "1212121212". I want to replace that by, lets say, 2323232323. As of now, nothing is changing when I run your script ;) Any idea why? Thanks again :>

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hmm thanks for this. What I meant by open it is I dont want to physically open it, and hit ctrl f..... What you provided looks like what I wanted. But, the file has "1212121212". I want to replace that by, lets say, 2323232323. As of now, nothing is changing when I run your script :> Any idea why? Thanks again :unsure:

Did you change the filenames to the correct files?

You can print the contents of what you read if you add ConsoleWrite($SH & @CRLF) after line 3 and 4, so you'll see the difference immediately.

With this you could also ensure that there is the "1212121212" in the original file.

Edited by Hannes123

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Did you change the filenames to the correct files?

You can print the contents of what you read if you add ConsoleWrite($SH & @CRLF) after line 3 and 4, so you'll see the difference immediately.

With this you could also ensure that there is the "1212121212" in the original file.

I see that it changed when I add ConsoleWrite($SH & @CRLF). But it is not saving. How can I save it as a different name (just prompt for file name).

Share this post


Link to post
Share on other sites

I see that it changed when I add ConsoleWrite($SH & @CRLF). But it is not saving. How can I save it as a different name (just prompt for file name).

Take a look at the second FileOpen() you'll need to set the right filename there, too.

You can prompt for the filename with InputBox for example or more comfortable: FileOpenDialog.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Take a look at the second FileOpen() you'll need to set the right filename there, too.

You can prompt for the filename with InputBox for example or more comfortable: FileOpenDialog.

Yes i know this. I have created another file, and I am choosing it, but still it is not changing in there. Question: does the script look for the hex or dec in the file? Which means

if I want to replace 1111, do I look for 1111 or 31 31 31 31 ?

Share this post


Link to post
Share on other sites

Yes i know this. I have created another file, and I am choosing it, but still it is not changing in there. Question: does the script look for the hex or dec in the file? Which means

if I want to replace 1111, do I look for 1111 or 31 31 31 31 ?

It just treats the bin data as a string. So look for 1111. In my example "246668" were the first three characters of the file (in this case "$fh").


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

It just treats the bin data as a string. So look for 1111. In my example "246668" were the first three characters of the file (in this case "$fh").

ok thanks. Here is my code:

$fh = FileOpen("C:\Documents and Settings\user\Desktop\old", 16)
$SH = FileRead($fh)
FileClose($fh)

$SH = StringReplace($SH, "36393639363936393639", "2222222222222222222")
 ConsoleWrite($SH & @CRLF)
$fh = FileOpen("C:\Documents and Settings\user\Desktop\new",  17)
FileWrite($fh, $SH)
FileClose($fh)

The console is showing the "22.." but the "new" file still has "3639.." ? :unsure: Thanks again!

Share this post


Link to post
Share on other sites

$fh = FileOpen("test2.au3", 2)
FileWrite($fh, "0x001122334455667788993639363936393639363999887766554433221100")
FileClose($fh)

$fh = FileOpen("test2.au3", 16)
$SH = FileRead($fh)
FileClose($fh)

ConsoleWrite($SH & @CRLF)
$SH = StringReplace($SH, "36393639363936393639", "22222222222222222222")
ConsoleWrite($SH & @CRLF)

$fh = FileOpen("test3.au3",  18)
FileWrite($fh, $SH)
FileClose($fh)

Works perfectly for me. The only error I found was that your searchstring had 20 characters and your replacestring had 19 so it would write a text file.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

ok thanks. Here is my code:

$fh = FileOpen("C:\Documents and Settings\user\Desktop\old", 16)
$SH = FileRead($fh)
FileClose($fh)

$SH = StringReplace($SH, "36393639363936393639", "2222222222222222222")
 ConsoleWrite($SH & @CRLF)
$fh = FileOpen("C:\Documents and Settings\user\Desktop\new",  17)
FileWrite($fh, $SH)
FileClose($fh)

The console is showing the "22.." but the "new" file still has "3639.." ? :unsure: Thanks again!

i figured out what it is doing. It is appending it to the end of the file, but it is writing them in string format. I want it to look the same, so how can i write them back in hex?

Share this post


Link to post
Share on other sites

Saw my last post?

1. Use FileOpen("File", 18)

2. Make sure searchstring and replacestring have the same length.

:unsure:


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Saw my last post?

1. Use FileOpen("File", 18)

2. Make sure searchstring and replacestring have the same length.

:unsure:

Thanks a lot for your help :>

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