读文件到十六进制的函数(Delphi 7 下可用) - 回复 "峰哥!!!" 的问题


问题来源: http://www.cnblogs.com/del/archive/2009/03/09/1284244.html#1472084


{函数}

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;


 
   

你可能感兴趣的:(Delphi)