1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
THexaLine = array[0..15] of byte;
TForm3 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
function HexaLineToChr(const HL : THexaLine; const Size : integer) : string;
var
P: PChar;
I: Integer;
begin
{ la taille de la chaine depend de Size }
SetLength(result, Size);
P := PChar(result);
for I := 0 to Size-1 do begin
case HL[I] of
{ si on est dans l'interval 32..126 on affiche le caractere correspondant }
32..126 : P[0] := chr(HL[I]);
else
{ sinon on affiche un point (.) }
P[0] := #46;
end;
Inc(P);
end;
end;
function HexaLineToStr(const HL : THexaLine; const Size : integer) : string;
var
P: PChar;
I: Integer;
const
Digits: array[0..16] of Char = '0123456789ABCDEF';
begin
{ le retour fait toujours 47 caracteres }
SetLength(result, 47);
P := PChar(result);
for I := 0 to 15 do begin
{ si on est inferieur a la taille du buffer }
if I < Size then begin
{ on convertis le byte en sa valeur chaine (hexa) }
P[0] := Digits[HL[I] shr 4];
P[1] := Digits[HL[I] and $F];
end else begin
{ sinon on mets deux underscores pour montrer a l'utilisateur qu'il n'y a plus rien }
P[0] := '_';
P[1] := '_';
end;
{ si on est inferieur a la taille du buffer -1 }
if I < Size-1 then begin
{ on ajoute un espace et on incremente P de 3 }
P[2] := #32;
Inc(P,3);
end else
{ sinon on incremente P de 2 (fin de ligne) }
Inc(P,2);
end;
end;
procedure ReadFile(const Filename : string; Strs : TStrings);
var Buffer : THexaLine; { le buffer }
Count,N : cardinal; { compteur offset de ligne | taille du buffer }
TSL : TStringList; { tstringlist temporaire pour ne pas ecrire directement dans Strs }
TFS : TFileStream; { }
begin
TSL := nil; { on nilliize pour eviter les erreur de liberation }
TFS := nil; { }
try
{ on crée la stringlist et on place les offsets de colones }
TSL := TStringList.Create;
TSL.Add(' 00 01 02 03 04 05 07 08 09 ');
{ on init count a 0 }
count := 0;
{ si le fichier existe }
if FileExists(FileName) then begin
{ on crée le stream et on ouvre le fichier en mode lecture seule }
TFS := TFileStream.Create(FileName, fmOpenRead);
{ on se place au debut (inutile mais plus sur) }
TFS.Seek(0,soFromBeginning);
{ boucle de lecture du fichier }
repeat
{ on lit le buffer et on recupere la taille lue }
N := TFS.Read(Buffer,SizeOf(Buffer));
{ on place le buffer dans une chaine qu'on ajoute dans la liste
offset ligne (hex) | valeur hexa (hex) | caracteres (char) }
TSL.Add( format('0x%.8x %s %s',[ Count,
HexaLineToStr(Buffer,N),
HexaLineToChr(Buffer,N)] ) );
{ on incremente le compteur avec la taille N }
inc(Count,N);
{ si le compteur est egale a la taille du stream (fin du fichier) on arrete la boucle }
until count = TFS.Size;
end;
{ on assigne les chaines de TSL a STRS }
Strs.Assign(TSL);
finally
{ on libere la liste temporaire et le stream }
TFS.Free;
TSL.Free;
end;
end;
procedure TForm3.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
ReadFile(OpenDialog1.FileName, ListBox1.Items);
end;
end. |
Partager