Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   int32_t , int16_t in Delphi, Function nach Delphi übersetzen (https://www.delphipraxis.net/190878-int32_t-int16_t-delphi-function-nach-delphi-uebersetzen.html)

bernhard_LA 15. Nov 2016 21:47

int32_t , int16_t in Delphi, Function nach Delphi übersetzen
 
wie würde man diesen Code in Delphi übersetzen


Delphi-Quellcode:
inline void gds (int32_t &i)
{
// ......
//  swap required
  char x;
  char *d = (char *)&i;
  x = d[0]; d[0] = d[3]; d[3] = x;
  x = d[1]; d[1] = d[2]; d[2] = x;

// ......
// other action
  i = (int32_t (((unsigned char *)&i) [0]) << 24) | (int32_t (((unsigned char *)&i) [1]) << 16) |
      (int32_t (((unsigned char *)&i) [2]) << 8) |  int32_t (((unsigned char *)&i) [3]);
#endif
}

BUG 15. Nov 2016 23:06

AW: int32_t , int16_t in Delphi, Function nach Delphi übersetzen
 
Da hast du ja wilden Code gefunden. Als erstes werden die Bytes von i umgedreht, wie bei einer Endianess-Konvertierung.

Der andere Abschnitt sieht ähnlich aus, hier wird wird aus dem Byte an der niedrigsten Adresse das signifikanteste Byte in einem 32bit IntegeR, usw... Könnte eine Konvertierung von Big-Endian zur Endianess des Sytems sein.

Namenloser 15. Nov 2016 23:45

AW: int32_t , int16_t in Delphi, Function nach Delphi übersetzen
 
Ich glaube, man könnte es so übersetzen:

Delphi-Quellcode:
function gds(i: LongInt): LongInt; inline;
var
  d: array[0..3] of Byte absolute i;
  x: Byte;
begin
// ......
//  swap required
  x := d[0]; d[0] := d[3]; d[3] := x;
  x := d[1]; d[1] := d[2]; d[2] := x;

// ......
// other action
  i := (Int32(d[0]) shl 24) or (Int32(d[1]) shl 16) or (Int32(d[2]) shl 8) or (Int32(d[3]));
{$endif}
end;
Ungetestet.

himitsu 16. Nov 2016 04:11

AW: int32_t , int16_t in Delphi, Function nach Delphi übersetzen
 
Da fehlt ein #IF zum #ENDIF :stupid:

Und ja, da war jemand bissl wirr im Kopf und hat die "selbe" Endianess-Konvertierung auf zwei Weisen umgesetzt.
Delphi-Quellcode:
procedure gds(var i: LongInt); inline;
begin
  // swap required
  i := ByteSwap(i); // sowas ähnliches, wie System.Swap
 
  // ......

  // other action
  i := ByteSwap(i);
end;
OHNE das "Heimliche" in den ... hebt sich das Gegenseitig auf und es kommt nix bei raus
Delphi-Quellcode:
procedure gds(var i: LongInt); inline;
begin
  //i := ByteSwap(i);
  //i := ByteSwap(i);
end;

bytecook 16. Nov 2016 07:38

AW: int32_t , int16_t in Delphi, Function nach Delphi übersetzen
 
Bytes swappen <asm>

Aus (http://www.delphipraxis.net/48884-fe...-delphi-2.html)

Code:
function SwapDWord(DW: DWord): DWord;
  asm
    {$IFDEF CPUX64}
    MOV EAX, ECX
    {$ENDIF CPUX64}
    BSWAP EAX
  end;
HTH!


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:44 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