Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Move() von PAnsiChar in ein Array Of Byte (https://www.delphipraxis.net/152320-move-von-pansichar-ein-array-byte.html)

H4ndy 18. Jun 2010 01:22

Delphi-Version: 2010

Move() von PAnsiChar in ein Array Of Byte
 
Folgendes Problem:

Ich habe einen X Byte langes PAnsiChar.
Dieses will ich einmal direkt auf Festplatte schreiben und einmal in ein lesbares Format konvertieren und dann ausgeben.

Das Schreiben auf Platte funktioniert gut und wuppt meiner Meinung nach auch korrekte Daten auf Festplatte.
Delphi-Quellcode:
thisEvent.Sysex: PAnsiChar
,
Delphi-Quellcode:
thisEvent.SysexLength: Integer
Delphi-Quellcode:
  fsSysex := TFileStream.Create(GetNextDumpFileName, fmCreate, fmShareDenyWrite);
  try
    fsSysex.Write(thisEvent.Sysex^, thisEvent.SysexLength);
  finally
    fsSysex.Free;
  end;
Nun moechte ich die gleichen Daten auch lesbar ausgeben (Hex).
Delphi-Quellcode:
SysExArray: Array of Byte;
Delphi-Quellcode:
  SetLength(SysExArray, thisEvent.SysexLength);
  Move(thisEvent.Sysex^, SysExArray[0], thisEvent.SysexLength);
  DbgOut(ByteArrayToHexString(SysExArray))
ByteArrayToHexString sieht so aus:
Delphi-Quellcode:
function TfrmMain.ByteArrayToHexString(const InArr: array of Byte): string;
var
  tmpStrList: TStringList;
  i: Integer;
begin
  tmpStrList := TStringList.Create;
  try
    for I := Low(InArr) to High(InArr) do
    begin
      tmpStrList.Append(IntToHex(InArr[i], 2));
    end;
    tmpStrList.Delimiter := ' ';
    Result := tmpStrList.DelimitedText;
  finally
    tmpStrList.Free;
  end;
end;
Frage ist jetzt: Was mach ich bei Move() falsch?

Ein Hexeditor zeigt mir die Datei so an:
Code:
F0 7E 00 06 01 F7
Mein ByteToArrayToHexString spuckt aber aus:
Code:
40 6A AF 00 A0 D9
Ersteres ist richtig (gueltige MIDI SysEx-Nachricht), zweiteres nur Müll.
Hab jetzt schon mit CopyMemory & Co. experimentiert, aber irgendwie klappts net :|

himitsu 18. Jun 2010 07:35

AW: Move() von PAnsiChar in ein Array Of Byte
 
Funktioniert problemlos. :gruebel:
Delphi-Quellcode:
var thisEvent_Sysex: PAnsiChar;
  thisEvent_SysexLength: Integer;
  SysExArray: Array of Byte;

procedure TForm1.FormCreate(Sender: TObject);
begin
  thisEvent_Sysex := #$F0#$7E#$00#$06#$01#$F7;
  thisEvent_SysexLength := 6;
  SetLength(SysExArray, thisEvent_SysexLength);
  Move(thisEvent_Sysex^, SysExArray[0], thisEvent_SysexLength);
  ShowMessage(ByteArrayToHexString(SysExArray));
end;

p80286 18. Jun 2010 16:24

AW: Move() von PAnsiChar in ein Array Of Byte
 
Warum so umständlich?
du hast ein PAnsiChar und die zugehörige Länge also warum nicht so

Delphi-Quellcode:
dummystr:='';
for i:=0 to länge-1 do
  dummystr:=inttohex(byte(pAnsiChar^[i]),2);
Gruß
K-H

shmia 18. Jun 2010 16:48

AW: Move() von PAnsiChar in ein Array Of Byte
 
Zitat:

Zitat von H4ndy (Beitrag 1029828)
Delphi-Quellcode:
  SetLength(SysExArray, thisEvent.SysexLength);
  Move(thisEvent.Sysex^, SysExArray[0], thisEvent.SysexLength);
  DbgOut(ByteArrayToHexString(SysExArray))

Delphi-Quellcode:
// vereinfachter Code
var
  SysExText:Ansistring;
..
SetString(SysExText, thisEvent.Sysex, , thisEvent.SysexLength);
DbgOut(StringToHexString(SysExText));
SetString() ist nicht so gefährlich wie Move().


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:47 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz