Delphi - Binär Datei in ein Memo laden

 type
  TMyByteArray = array of byte;

const
  BUFFERSIZE = 2048;
  HEXPREFIX = '$';
  HEXSUFFIX = ' ';

function BuffToHex(ByteArray: TMyByteArray): string;
var
  i: Integer;
  s: string;
  foo: string;
  HexLength: Integer;
const
  HEXSTR = '0123456789ABCDEF';
begin
  HexLength := Length(HEXPREFIX) + 2 + Length(HEXSUFFIX);
  SetLength(foo, Length(ByteArray) * HexLength);
  for i := 0 to High(ByteArray) do
  begin
    s := HEXPREFIX + HEXSTR[((ByteArray[i] and $F0) shr 4) + 1] +
      HEXSTR[(ByteArray[i] and $0F) + 1] + HEXSUFFIX;
    Move(s[1], foo[i * HexLength + 1], HexLength);
  end;
  Result := foo;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  fs: TFileStream;
  Buffer: TMyByteArray;
  BytesRead: Longint;
  s: string;
  i: Int64;
  HexLength: Integer;
begin
  i := 0;
  SetLength(Buffer, BUFFERSIZE);
  fs := TFileStream.Create('G:\MP3s\Beatles\Beatles -  Blue Jay Way.mp3',
    fmOpenRead);
  HexLength := Length(HEXPREFIX) + 2 + Length(HEXSUFFIX);
  SetLength(s, fs.size * HexLength);
  Progressbar1.Max := fs.Size div BUFFERSIZE;
  try
    repeat
      BytesRead := fs.Read(Buffer[0], BUFFERSIZE);
      Move(BuffToHex(Buffer)[1], s[i * (HexLength * BUFFERSIZE) + 1],
        HexLength * BytesRead);
      Inc(i);
      Progressbar1.StepIt;
      Application.ProcessMessages;
    until BytesRead < BUFFERSIZE;
    Memo1.Text := s;
  finally
    FreeAndNil(fs);
  end;
end;

 

2012-01-26T23:14:41 +0100, mail[at]michael[Bindestrich]puff.de