unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, Math;<br><br>type<br> TForm1 = class(TForm)<br> OpenDialog1: TOpenDialog;<br> Button5: TButton;<br> Label1: TLabel;<br> Edit1: TEdit;<br> Button6: TButton;<br> edt1: TEdit;<br> edt2: TEdit;<br><br> procedure Button5Click(Sender: TObject);<br> procedure Button6Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function ReadHex(fn: string; Pos: Integer): string;<br>var<br> Source: file;<br> Buffer: array[0..1] of byte;<br> Buffer0: PChar;<br> Read: integer;<br>begin<br> AssignFile(Source, fn);<br> try<br> Reset(Source, 1);<br> GetMem(Buffer0, pos);<br> try<br> //BlockRead(Source, Buffer0^, Pos);<br> BlockRead(Source, Buffer, 2, Read);<br> Result := Format('%.2x %.2x', [Buffer[0], Buffer[1]]);<br> finally<br> FreeMem(Buffer0);<br> CloseFile(Source);<br> end;<br> except<br> MessageDlg('无法打开输出文件!', mtInformation, [mbOK], 0);<br> Exit;<br> end;<br>end;<br><br>function Hex2Dec(Value: string): integer;<br>var<br> c: char;<br> nIndex, nLength: integer;<br>begin<br> Result := 0;<br> nLength := Length(Value);<br> for nIndex := 0 to nLength - 1 do<br> begin<br> c := Value[nLength - nIndex];<br> if ((c >= 'A') and (c <= 'F')) then<br> inc(Result, (ord(c) - 55) * Trunc(Power(16, nIndex)))<br> else if ((c >= '0') and (c <= '9')) then<br> inc(Result, (ord(c) - 48) * Trunc(Power(16, nIndex)));<br> end;<br>end;<br><br>procedure SetHex(FileName: string; StartPos: Integer; hex1, hex2: string);<br>var<br> Ori: TFileStream;<br> Tmp: TMemoryStream;<br> TempFile: string;<br> EndPos: Integer;<br> swap_char: char;<br>begin<br> EndPos := StartPos + 2;<br> Ori := TFileStream.Create(FileName, fmOpenRead);<br> Tmp := TMemoryStream.Create;<br> if (StartPos <> 0) then<br> Tmp.CopyFrom(Ori, StartPos);<br><br> swap_char := Char(Hex2Dec(hex1));<br> Tmp.WriteBuffer(swap_char, sizeof(swap_char));<br> swap_char := Char(Hex2Dec(hex2));<br> Tmp.WriteBuffer(swap_char, sizeof(swap_char));<br><br> Ori.Seek(EndPos, soFromBeginning);<br> Tmp.CopyFrom(Ori, Ori.Size - EndPos);<br><br> TempFile := ExtractFilePath(FileName) + 'cxz.txt';<br> Tmp.SaveToFile(tempfile);<br> FreeAndNil(Ori);<br> FreeAndNil(Tmp);<br> DeleteFile(pchar(FileName));<br> RenameFile(TempFile, FileName);<br>end;<br><br>procedure TForm1.Button5Click(Sender: TObject);<br>begin<br> // showmessage(inttostr(Hex2Dec(Edit1.Text)));<br> Label1.Caption := ReadHex('1.exe', Hex2Dec(Edit1.Text));<br>end;<br><br>procedure TForm1.Button6Click(Sender: TObject);<br>begin<br> //SetHex('1.exe', Hex2Dec(Edit1.Text), 5, 12);<br> SetHex('1.exe', Hex2Dec(Edit1.Text),edt1.Text, edt2.Text);<br>end;<br><br>end.