Author: Jonas Bilinkevicius
How can I read a file and get the first 10 bytes of that file in its hexadecimal
format?
Answer:
1 function FirstTenBytes(const sFile: TFileName): string;
2 var3 oIn: TFileStream;
4 iRead: Integer;
5 iMaxRead: Integer;
6 iData: Byte;
7 begin8 Result := '';
9 oIn := TFileStream.Create(sFile, fmOpenRead or fmShareDenyNone);
10 try11 iMaxRead := 10;
12 if iMaxRead > oIn.Size then13 iMaxRead := oIn.Size;
14 for iRead := 1 to iMaxRead do15 begin16 oIn.read(iData, 1);
17 Result := Result + IntToHex(iData, 2);
18 end;
19 finally20 FreeAndNil(oIn);
21 end;
22 end;