Solved

search and overwrite binary files data

Posted on 2003-10-23
6
187 Views
Last Modified: 2010-04-05
how i can replace a string into binary files BUT the 2 two strings haven't same length?

eg. old string = "Hi"
new string = "Hello"

example??

bye , tia86

125 points for the correct answer
0
Comment
Question by:Tia86
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Author Comment

by:Tia86
ID: 9608134
i have found this code

procedure TForm1.Button1Click(Sender: TObject);
var
  f: file;
  l: Longint;
  FileName, oldstring, newstring, s: string;
begin
  oldstring := 'old_string';
  newstring := 'new_string';
  FileName  := 'file.xyz';
   
  s := oldstring;
  AssignFile(f, FileName);
  Reset(f, 1);
  for l := 0 to FileSize(f) - Length(oldstring) - 1 do
  begin
    Application.ProcessMessages;
    Seek(f, l);
    BlockRead(f, oldstring[1], Length(oldstring));
    if oldstring = s then
    begin
      Seek(f, l);
      BlockWrite(f, newstring[1], Length(newstring));
      ShowMessage('String successfully replaced!');
    end;
    Application.ProcessMessages;
  end;
  CloseFile(f);
end;


but don't work if the 2 strings haven't same lenght...

help me!
0
 
LVL 3

Accepted Solution

by:
BJThomsen earned 43 total points
ID: 9608453
Simply copy the file from one file to another when you get to the string write the new string then the rest of the file.

This could result in corrupt data though. Not sure what the binary data is, but any positional references within the file could now be incorrect. If it was part of a structure the new format may not correctly read ect.

0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 41 total points
ID: 9613446
Be very careful! Some binary formats depend on string values having a fixed length! There could be a length byte in front of the 'Hi' string or the system expects a fixed length and will consider 'llo' as part of some new data element instead as part of your 'Hello' string value. Especially executables are very sensitive about this. (And some executables maintain a checksum that will invalidate the exe if you modify data inside them.)
0
 
LVL 1

Assisted Solution

by:ttd
ttd earned 41 total points
ID: 9640460
Hi Tia86,

The answer is simply: No way!

Do you want to make your shin-bone 2 inches longer without "modifying" your leg? So modifying things is simple, but to keep the modified things working is not so simple unless you have a full knowledge about the thing that is being modified.

Regards,

ttd
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question