delphi 读文件到十六进制的函数(Delphi 7 下可用)

2018-10-31

{函数}
function ReadFileToHex(FileName: string): string;
var
b: Byte;
begin
Result := '';
if not FileExists(FileName) then Exit;
with TMemoryStream.Create do begin
LoadFromFile(FileName);
Position := 0;
while Position < Size do
begin
ReadBuffer(b, 1);
Result := Result + Format('%.2x ', [b]);
end;
Trim(Result);
Free;
end;
end;

{调用}
procedure TForm1.Button1Click(Sender: TObject);
var
str: string;
begin
str := ReadFileToHex('c:\temp\test.txt');
ShowMessage(str);
end;

阅读33