unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo; {需要添加兩個 Memo 用於顯示}
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//先制造一個測試文件
procedure TForm1.FormCreate(Sender: TObject);
var
strList: TStringList;
begin
strList := TStringList.Create;
strList.Add('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
strList.SaveToFile('c:\temp\test.txt');
strList.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ms: TMemoryStream;
c: Char;
s1,s2: string;
begin
ms := TMemoryStream.Create;
ms.LoadFromFile('c:\temp\test.txt'); {讀入內存流}
s1 := '';
s2 := '';
ms.Position := 0; {指針到開始}
while ms.Position < ms.Size do {循環讀出}
begin
ms.Read(c,1); {每讀出一個字節, 指針會自動移到新的位置}
s1 := s1 + c + ' '; {用文本記錄}
s2 := s2 + IntToHex(Byte(c),2) + ' '; {用兩位數的十六進制記錄}
end;
Memo1.Lines.Text := s1;
Memo2.Lines.Text := s2;
{Memo1 會顯示: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z }
{Memo2 會顯示: 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 0D 0A}
ms.Free;
end;
end.