Thursday, November 8, 2012

Read a binary file and display it in Hex value.

procedure TForm1.ReadFINFile;
var
  FStream: TFileStream;
  Buff : Byte;
  iCount: Word;
  nRead, iOffset, iTotal: Integer;
  zByte, zLine: String;
begin
  iCount := 0;
  iOffset := 0;
  iTotal := 0;
  zLine := FormatFloat(`00000000`,0) + ` `;
  Memo1.Clear;
  FStream := TFileStream.Create(FileName, fmOpenRead);
  try
    repeat
      nRead := FStream.Read(Buff, SizeOf(Buff));
      if nRead<>0 then
       begin
         Inc(iTotal,nRead);
         //Buff := ReverseDWord(Buff);
         zByte := IntToHex(Buff,1);
         if Length( zByte ) = 1 then
            zByte := `0` + zByte;
         Inc(iCount);
         zLine := zLine + zByte + ` `;
         if (iCount = SpinEdit1.Value) or

            (iTotal = FStream.Size) then
             begin
               Memo1.Lines.Add(zLine);
               Inc(iOffset,16);
               zLine := IntToHex(iOffset,8) + ` `;
               //zLine := ``;
               iCount := 0;
             end;
       end;
    until (nRead = 0);
  finally
    FStream.Free;
  end;
  // Set cursor position :
  Memo1.SelStart := 0;
  // Go to cursor (have it visible) :
  SendMessage(Memo1.Handle, EM_SCROLLCARET, 0, 0);
end;



No comments:

Post a Comment