Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.059 Beiträge
 
Delphi 10.4 Sydney
 
#3

AW: Einbinden von OSCUtils in eigenes Programm

  Alt 13. Sep 2021, 11:13
Das liegt an der Änderung in - ich glaube - XE3 zu XE4, wo die Definition von TBytes geändert wurde.
War es früher ein TBytes = array of byte; ist es nun ein TBytes = TArray<Byte>; .
Die Definition von TIdBytes blieb bei der alten Variante, so dass es für den neueren Compiler verschiedene Typen sind.

Siehe auch hier:
https://stackoverflow.com/questions/...-tidbytes?rq=1

Wenn du dir die beiden Funktionen aus IdGlobal einfach rauskopierst und unter implementation einfügst und die uses-Klausel, Funktionsdefinition sowie Aufrufe abänderst, sollte das schon alles von alleine laufen:

Delphi-Quellcode:
implementation

uses
  Math, {$IFNDEF FPC}WinSock {$ENDIF};

  {$IFNDEF DOTNET}
function RawToBytes(const AValue; const ASize: Integer): TBytes;
{$IFDEF USE_INLINE}inline;{$ENDIF}
begin
  SetLength(Result, ASize);
  if ASize > 0 then begin
    Move(AValue, Result[0], ASize);
  end;
end;
{$ENDIF}

procedure CopyTIdBytes(const ASource: TBytes; const ASourceIndex: Integer; // Anmerkung TiGü -> Richtig wäre hier jetzt natürlich die Umbenennung in CopyTBytes/CopyBytes
  var VDest: TBytes; const ADestIndex: Integer; const ALength: Integer);
{$IFDEF USE_INLINE}inline;{$ENDIF}
begin
  {$IFDEF DOTNET}
  System.array.Copy(ASource, ASourceIndex, VDest, ADestIndex, ALength);
  {$ELSE}
  //if these asserts fail, then it indicates an attempted buffer overrun.
  Assert(ASourceIndex >= 0);
  Assert((ASourceIndex+ALength) <= Length(ASource));
  Move(ASource[ASourceIndex], VDest[ADestIndex], ALength);
  {$ENDIF}
end;

function MakeOSCFloat(value: Single): TBytes;
var
  intg: Integer;
begin
  intg := PInteger(@value)^;

  {$IFDEF FPC}
  intg := BEtoN(intg);
  {$ELSE}
  intg := htonl(intg);
  {$ENDIF}

  Result := RawToBytes(intg, SizeOf(intg));
end;

function MakeOSCInt(value: Integer): TBytes;
begin
  {$IFDEF FPC}
  value := BEtoN(value);
  {$ELSE}
  value := htonl(value);
  {$ENDIF}
  Result := RawToBytes(value, SizeOf(value));
end;

function MakeOSCString(value: String): TBytes;
var i, ln: Integer;
begin
  ln := TEncoding.UTF8.GetByteCount(value);
  ln := ln + (4 - ln mod 4);
  SetLength(Result, ln);
  ln := TEncoding.UTF8.GetBytes(value, 1, Length(value), Result, 0);
  for i := ln to High(Result) do
    result[i] := 0;
end;

function UnpackInt(Bytes: TBytes; var Offset: Integer): TBytes;
var
  i: Integer;
begin
  SetLength(Result, SizeOf(Integer));
  // Copy bytes and change byte order
  for i := 0 to High(Result) do
    Result[i] := Bytes[Offset + High(Result) - i];
  Inc(Offset, SizeOf(Integer));
end;

function UnpackString(Bytes: TBytes; var Offset: Integer): TBytes;
var
  off: Integer;
begin
  // Strings are null terminated. Find position of null.
  off := Offset;
  while (off < Length(Bytes)) and (Bytes[off] <> 0) do
    Inc(off);
  // Retrieve the string.
  SetLength(Result, off - Offset);
  CopyTIdBytes(Bytes, Offset, Result, 0, Length(Result));
  // Increase the offset by a multiple of 4.
  Offset := off + (4 - off mod 4);
end;
  Mit Zitat antworten Zitat