Hola elmago00.
Cita:
Empezado por elmago00
quiero editar el archivo en hexadecimal y guardarlo todo. guardar todo el archivo hexadecimal pero ya modificado y en su extensión original.
para que funciono como antes con la modificacion hecha.
|
Hola elmago00.
Este código permite abrir, modificar, buscar texto obre la columna ASCII y guardar el archivo modificado con el mismo nombre u otro.
Código Delphi
[-]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, XPMan, Menus, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
FindDialog1: TFindDialog;
Archivo1: TMenuItem;
miOpen: TMenuItem;
miSave: TMenuItem;
Buscartexto1: TMenuItem;
Panel1: TPanel;
Label1: TLabel;
ComboBox1: TComboBox;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure miOpenClick(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure Buscartexto1Click(Sender: TObject);
procedure FindDialog1Find(Sender: TObject);
procedure miSaveClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FStream: TMemoryStream;
FColAsc: TStrings;
FFind: Integer;
procedure DumpFile(aFileName:TFileName; const BPF: Word);
procedure SaveDump(aFileName:TFileName; const BPF: Word);
public
end;
var
Form1: TForm1;
implementation {$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.CommaText:= '8,10,16,24,32,48,64,128,255';
ComboBox1.ItemIndex:= 2;
FStream:= TMemoryStream.Create;
FColAsc:= TStringList.Create;
with StringGrid1 do
begin
Options:= Options-[goVertLine]-[goHorzLine]+[goEditing];
Font.Name:= 'Courier';
Font.Size:= 10;
Col:= 1;
Row:= 1;
end;
end;
procedure TForm1.miOpenClick(Sender: TObject);
begin
with OpenDialog1 do
begin
Filter:= '*.*';
if Execute then
begin
Caption:= FileName;
DumpFile(FileName, StrToInt(ComboBox1.Items[ComboBox1.ItemIndex]) );
end;
end;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
with TStringGrid(Sender) do
CanSelect:= ((ACol>0)and(ACol < ColCount-1) and
((ARow>= FixedRows)and(ARow < RowCount)));
end;
procedure TForm1.miSaveClick(Sender: TObject);
begin
with SaveDialog1 do
begin
Filter:= '*.*';
if Execute then SaveDump(FileName, 16);
end;
end;
procedure TForm1.Buscartexto1Click(Sender: TObject);
begin
with FindDialog1 do
begin
FFind:= 0;
Execute;
end;
end;
procedure TForm1.FindDialog1Find(Sender: TObject);
begin
while(FFind < FColAsc.Count)and(Pos(FindDialog1.FindText,FColAsc[FFind])=0) do
Inc(FFind);
if (FFind < FColAsc.Count-1) then
begin
Inc(FFind);
StringGrid1.Row:= FFind;
StringGrid1.Col:= 1;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FStream.Free;
FColAsc.Free;
end;
procedure TForm1.DumpFile(aFileName:TFileName; const BPF: Word);
var
c, i, r: integer;
ascii: string;
aux: Byte;
begin
FStream.LoadFromFile(aFileName);
with StringGrid1 do
begin
ColWidths[0]:= 80;
Cells[0,0]:= 'Offset (h)';
ColCount:= BPF+2;
ColWidths[ColCount-1]:= Canvas.TextWidth('X')*BPF+10;
Cells[ColCount-1,0]:= 'ASCII';
for i:= 0 to BPF-1 do
begin
ColWidths[i+1]:= 30;
Cells[i+1,0]:= IntToHex(i, 2);
end;
RowCount:= FStream.Size div BPF+1;
FStream.Seek(0, soBeginning);
c:= 0;
r:= FixedRows;
while c < FStream.Size do
begin
Cells[0, r]:= Format('%s',[IntToHex(c, 8)]);
ascii:= EmptyStr;
i:= 0;
while (i < BPF) and(i+c < FStream.Size) do
begin
FStream.Read(aux, SizeOf(Byte));
Cells[i+1,r]:= IntToHex(aux,2);
if aux in [$0..$0D,$80..$90] then aux:= Ord('.');
ascii:= ascii + Chr(aux);
Inc(i);
end;
FColAsc.Add(ascii);
Cells[ColCount-1,r]:= ascii;
Inc(c, BPF);
Inc(r);
end;
end;
end;
procedure TForm1.SaveDump(aFileName:TFileName; const BPF: Word);
var
MS: TMemoryStream;
c,r: Integer;
buff: array of Byte;
begin
with StringGrid1 do
begin
MS:= TMemoryStream.Create;
try
SetLength(buff, BPF);
for r:= FixedRows to RowCount-1 do
begin
for c:= 0 to BPF-1 do buff[c]:= StrToInt('$'+Cells[c+1, r]);
MS.WriteBuffer(buff[0], BPF);
end;
MS.SaveToFile(aFileName);
finally
SetLength(buff,0);
MS.Free;
end;
end;
end;
end.
Lamento no haberlo escrito para FireMonkey pero solo cuento con Delphi 7. Con seguridad lo podrás adaptar del modo como comentaste en el mensaje
#19.
En todas las pruebas que realizé, modificando constantes de texto, el ejecutable resultante funcionó de modo correcto mostrando los valores modificados.
Descarga desde FTP
Club Delphi:
EditorHexadecimalBásico
Saludos