如何以16进制修改文件,比如将某exe文件第1字节'4D'修改为'FF' (100分)

  • 主题发起人 主题发起人 hlsoft
  • 开始时间 开始时间
H

hlsoft

Unregistered / Unconfirmed
GUEST, unregistred user!
如何以16进制修改文件,
比如将某exe文件的第一字节的值“4D”改成“FF”,就像UltraEdit那样?
 
你可以用文件流的wirteBuffer
 
具体代码怎么些呢?
 
愿闻其祥
 
多看帮助!采用fileopen,fileseek等函数

The following example uses a button, a string grid, and an Open dialog box on a form. When the button is clicked, the user is prompted for a filename. When the user clicks OK, the specified file is opened, read into a buffer, and closed. Then the buffer is displayed in two columns of the string grid. The first column contains the character values in the buffer. The second column contains the numeric values of the characters in the buffer.

procedure TForm1.Button1Click(Sender: TObject);

var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffer: PChar;
i: Integer
begin
if OpenDialog1.Execute then
begin
try
iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
iFileLength := FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffer := PChar(AllocMem(iFileLength + 1));
iBytesRead = FileRead(iFileHandle, Buffer, iFileLength);
FileClose(iFileHandle);

for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[1,i+1] := Buffer;
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer));
end;
finally
FreeMem(Buffer);
end;
end;
end;
 
我看不懂上面的例子,我只需要实现"将某exe文件的第一字节的值“4D”改成“FF"的代码就可以了.
 
简单参考。

procedure TfrmMain.btnWriteClick(Sender: TObject);
var
S:TMemoryStream;
begin
S:=TmemoryStream.Create;
S.LoadFromFile('Temp.exe');
S.Seek($0000,soFromBeginning);//
S.Write(FF,2);
S.savetoFile('Temp.exe');//保存
S.Free;
end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部