Einzelnen Beitrag anzeigen

hathor
(Gast)

n/a Beiträge
 
#9

AW: Datei als HEX laden (Suche schon mehrfach benutzt)

  Alt 16. Nov 2014, 19:29
Hier ist ein brauchbarer Code:
http://stackoverflow.com/questions/6...view-of-a-file

Wenn man im Memo den Font LUCIDA CONSOLE verwendet und den Code etwas ändert, kommt was Schönes heraus.

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function HexB (b: Byte): String;
 const HexChar: Array[0..15] of Char = '0123456789ABCDEF';
 begin
  result:= HexChar[b shr 4]+HexChar[b and $0f];
 end;

procedure HexDump(var data; size: Integer; s: TStrings);
 const
  sepHex=' ';
  sepAsc=' ';
  nonAsc='.';
 var
  i : Integer;
  hexDat, ascDat : String;
  buff : Array[0..1] of Byte Absolute data;

 begin
  hexDat:='';
  ascDat:='';
  for i:=0 to size-1 do
   begin
    hexDat:=hexDat+' '+HexB(buff[i]);
    if ((buff[i]>31) and (buff[i]<>255)) then
      ascDat:=ascDat+ ANSIChar(buff[i])
    else
      ascDat:=ascDat+nonAsc;
    if (((i+1) mod 16)<>0) and (((i+1) mod 8)=0) then
      hexDat:=hexDat+' '+sepHex;
    if ((i+1) mod 16)=0 then
     begin
      s.Add(hexdat+sepAsc+' '+ascdat);
      hexdat:='';
      ascdat:='';
     end;
   end;
  if (size mod 16)<>0 then
   begin
    if (size mod 16)<8 then
      hexDat:=hexDat+StringOfChar(' ',(8-(size mod 8))*2)
              +sepHex+StringOfChar(' ',16)
    else
      hexDat:=hexDat+StringOfChar(' ',(16-(size mod 16))*2);
    s.Add(hexDat + sepAsc + ascDat);
   end;
 end;
(*
procedure TForm1.Button1Click(Sender: TObject);
var
  FStream: TFileStream;
  buff: array[0..$fff] of Byte;
  nRead: Integer;
begin
  FStream := TFileStream.Create(edit1.text, fmOpenRead or fmShareDenyWrite);
  try
    repeat
      nRead := FStream.Read(Buff, SizeOf(Buff));
      if nRead<>0 then
        hexdump(buff, nRead, memo1.lines);
    until nRead=0;
  finally
    FStream.Free;
  end;
end;
*)

//Mit ListBox ist es viel schneller !! <---------------------------------
procedure TForm1.Button1Click(Sender: TObject);
 var
  FStream: TFileStream;
  buff: array[0..$fff] of Byte;
  nRead: Integer;
 begin
 LB1.Items.Clear; LB1.Items.BeginUpdate;
  FStream := TFileStream.Create(edit1.text, fmOpenRead or fmShareDenyWrite);
  try
    repeat
      nRead := FStream.Read(Buff, SizeOf(Buff));
// if nRead<>0 then hexdump(buff, nRead, memo1.lines);
      if nRead<>0 then hexdump(buff, nRead, LB1.Items);
    until nRead=0;
  finally
    FStream.Free;
  end;
 LB1.Items.Endupdate;
 end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1Click(Self);
end;
end.
Miniaturansicht angehängter Grafiken
bin2hex.jpg  

Geändert von hathor (16. Nov 2014 um 23:25 Uhr)
  Mit Zitat antworten Zitat