2008. november 22., szombat

How to read a file in binary mode


Problem/Question/Abstract:

How can I read a file and get the first 10 bytes of that file in its hexadecimal format?

Answer:

function FirstTenBytes(const sFile: TFileName): string;
var
  oIn: TFileStream;
  iRead: Integer;
  iMaxRead: Integer;
  iData: Byte;
begin
  Result := '';
  oIn := TFileStream.Create(sFile, fmOpenRead or fmShareDenyNone);
  try
    iMaxRead := 10;
    if iMaxRead > oIn.Size then
      iMaxRead := oIn.Size;
    for iRead := 1 to iMaxRead do
    begin
      oIn.Read(iData, 1);
      Result := Result + IntToHex(iData, 2);
    end;
  finally
    FreeAndNil(oIn);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése