Einzelnen Beitrag anzeigen

Muetze1
(Gast)

n/a Beiträge
 
#6

Re: Hex-Darstellung einer binären Datei

  Alt 12. Jun 2004, 16:11
Moin!



Delphi-Quellcode:
type
  TMyByteArray = array of byte;

const
  HEXWIDTH = 2;
  BUFFERSIZE = 8;

function BuffToHex(ByteArray: TMyByteArray): string;
var
  i: Integer;
  s: String;
  foo: string;
begin
  Setlength(foo, length(ByteArray) * (HEXWIDTH+2));
  for i := 0 to length(ByteArray) - 1 do
  begin
    s := '$'+IntToHex(ord(ByteArray[i]), HEXWIDTH) + ' ';
    Move(s[1], foo[i * (HEXWIDTH+2) + 1], length(s));
  end;
  result := foo;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  fs: TFileStream;
  Buffer: TMyByteArray;
  BytesRead: Longint;
  s: String;
  i: Int64;
begin
  i := 0;
  SetLength(Buffer, BUFFERSIZE);
  fs := TFileStream.Create('G:\MP3s\Beatles\Beatles - Blue Jay Way.mp3',
    fmOpenRead);
  SetLength(s, fs.size*(HEXWIDTH+2));
  Progressbar1.Max := fs.Size div BUFFERSIZE;
  try
    repeat
      BytesRead := fs.Read(Buffer[0], BUFFERSIZE);
      Move(BuffToHex(Buffer)[1], s[i*((HEXWIDTH+2)*BUFFERSIZE)+1], (HEXWIDTH+2)*BUFFERSIZE);
      Inc(i);
      Progressbar1.StepIt;
      Application.ProcessMessages;
    until BytesRead < BUFFERSIZE;
    Memo1.Text := s;
  finally
    FreeAndNil(fs);
  end;
end;
/EDIT: Die Grösse beim Move() musste auch noch angepasst werden...

MfG
Muetze1
  Mit Zitat antworten Zitat