Ver Mensaje Individual
  #34  
Antiguo 09-05-2014
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
elmago00,

Cita:
Empezado por ecfisa
...Lamento no haberlo escrito para FireMonkey pero solo cuento con Delphi 7...


Cita:
Empezado por elmago00
...¿Ahora si conteste a sus preguntas?...


Te comento: No puedes salvar la representación Hexadecimal del archivo cargado en el TStringGrid y que este tenga los atributos y comportamiento del original, debes salvar el archivo y sus modificaciones en Binario.

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,
  System.Variants, System.UIConsts, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs,
  FMX.StdCtrls, FMX.ListBox, FMX.Layouts, FMX.Grid;

type

  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    ComboBox1: TComboBox;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Button1: TButton;
    Button2: TButton;
    StatusBar1: TStatusBar;
    Label1: TLabel;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    SaveDialog2: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure DumpFile(aFileName:TFileName; SG: TStringGrid; const BPF: Word);
    procedure SaveDumpText(aFileName:TFileName; SG: TStringGrid);
    procedure SaveDumpBinary(aFileName:TFileName; const BPF: Word);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  CancelProcess : Boolean;

implementation

{$R *.fmx}

procedure TForm1.DumpFile(aFileName:TFileName; SG: TStringGrid; const BPF: Word);
var
  c, i, r, b : Integer;
  Ascii: String;
  Aux: Byte;
  Col : Integer;

begin

  with TMemoryStream.Create do
  try

    LoadFromFile(aFileName);

    Caption:= Format('FileName = %s , FileSize = %d Bytes', [aFileName, Size]);

    // Configurar StringGrid

    SG.ShowHeader := False;
    SG.ShowHorzLines := False;
    SG.ShowVertLines := False;

    for i := 1 to BPF + 2 do
       SG.AddObject(TStringColumn.Create(nil));

    SG.RowCount := (Size div BPF) + 1;
    if (Size Mod BPF) <> 0 then
       SG.RowCount := SG.RowCount + 1;

    for Col := 0 to SG.ColumnCount - 1 do
    begin

        if (Col = 0) then
           SG.Columns[Col].Width := 80;

        if (Col <> SG.ColumnCount - 1) and (Col <> 0) then
           SG.Columns[Col].Width := 30;

        if (Col = SG.ColumnCount - 1) then
           SG.Columns[Col].Width := SG.Canvas.TextWidth('X') * BPF;

    end;

    SG.Cells[0,0]:= 'Offset(h)';
    SG.Cells[SG.ColumnCount-1,0]:= 'ASCII';

    for i:= 0 to BPF-1 do
    begin
      SG.Cells[i+1,0]:= IntToHex(i,2);
    end;

    // Cargar en StringGrid

    Seek(0, soBeginning);
    c:= 0;
    r:= 1;
    b := 0;

    while c < Size do
    begin

      Application.ProcessMessages;

      if CancelProcess then
      begin
         CancelProcess := False;
         Break;
      end;

      SG.Cells[0, r]:= Format('%s',[IntToHex(c, 8)]);
      Ascii:= EmptyStr;
      i:= 0;

      while (i < BPF) and (b < Size) do
      begin
        Read(Aux, SizeOf(Byte));
        SG.Cells[i+1,r]:= IntToHex(Aux,2);
        if Aux = 0 then Aux := 46;
        Ascii:= Ascii + Chr(Aux);
        Inc(i);
        if (b < Size) then Inc(b);
      end;

      SG.Cells[SG.ColumnCount-1,r]:= Format('%s',[Ascii]);
      Inc(c, BPF);
      Inc(r);
      Label1.Text := Format('Procesado Byte %d de %d',[b, Size]);

    end;

  finally

    Free;

  end;

end;

// Salvar StringGrig a TXT
procedure TForm1.SaveDumpText(aFileName:TFileName; SG: TStringGrid);
var
  c,r: Integer;
  s: String;
begin
  with TStringList.Create do
  try
    for r:= 0 to SG.RowCount-1 do
    begin
      s:= '';
      for c:= 0 to SG.ColumnCount-1 do s:= s + SG.Cells[c,r] + ' ';
      SetLength(s,Length(s)-1);
      Add(s);
    end;
    SaveToFile(aFileName);
  finally
    Free;
  end;
end;

// Salvar StringGrig a Binario
procedure TForm1.SaveDumpBinary(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:= 1 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;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items.CommaText:= '8,10,16,24,32,48,64,128,255';
  ComboBox1.ItemIndex:= 2;
end;

// Carga un Archivo en Modo Hexadecimal en un StringGrid
procedure TForm1.Button1Click(Sender: TObject);
begin
  with OpenDialog1 do
  begin
    Filter:= '*.*';
    if Execute then
    begin
      DumpFile(FileName, StringGrid1,
        StrToInt(ComboBox1.Items[ComboBox1.ItemIndex]) );
    end;
  end;
end;

// Salva un Archivo en Modo Hexadecimal en un StringGrid a TXT
procedure TForm1.Button2Click(Sender: TObject);
begin
  with SaveDialog1 do
  begin
    Filter:= '*.txt';
    DefaultExt:= '*.txt';
    if Execute then
      SaveDumpText(FileName, StringGrid1);
  end;
end;

// Cancela Carga de un Archivo en Modo Hexadecimal en un StringGrid
procedure TForm1.Button3Click(Sender: TObject);
begin
   CancelProcess := True;
end;

// Inicializa el StringGrid
procedure TForm1.Button4Click(Sender: TObject);
var
   i : Integer;
begin
   StringGrid1.RowCount := 0;
   for i := StringGrid1.ColumnCount - 1 downto 0 do
         StringGrid1.Columns[i].DisposeOf;
end;

// Salva un Archivo en Modo Hexadecimal en un StringGrid a Binario
procedure TForm1.Button5Click(Sender: TObject);
begin
  with SaveDialog1 do
  begin
    Filter:= '*.*';
    if Execute then SaveDumpBinary(FileName, 16);
  end;
end;

end.
El código anterior en Delphi XE4 bajo Windows 7 Professional x32, es la versión 3 del código propuesto en el Msg #15 con las correcciones incluidas del Msg #18, el cual te permitirá: Cargar, Representar y Editar archivos visualmente en Hexadecimal y salvarlos posteriormente en su representación Hexadecimal (Modo Texto) o Binaria, como se muestra en la siguiente imagen:



El código esta disponible en : Visualizador de Archivos en Hexadecimal en FireMonkey v3

Suerte en tu proyecto

Nelson.

Última edición por nlsgarcia fecha: 09-05-2014 a las 17:57:04.
Responder Con Cita