Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Typecasting eines Arrays von Byte und Schreiben in verschiedene Variablen in Delphi (https://www.delphipraxis.net/203494-typecasting-eines-arrays-von-byte-und-schreiben-verschiedene-variablen-delphi.html)

himitsu 25. Feb 2020 14:02

AW: Typecasting eines Arrays von Byte und Schreiben in verschiedene Variablen in Delp
 
Swap

und in der System-Unit versteckt sich auch etwas (intern), wenn ich mich nicht irre. (wegen MacOS, was andersrum ist/war/oderso)

dummzeuch 25. Feb 2020 14:19

AW: Typecasting eines Arrays von Byte und Schreiben in verschiedene Variablen in Delp
 
Zitat:

Zitat von Moien (Beitrag 1458282)
Habt ihr eine Idee, wie man die Byte-Reihenfolge ändern kann?

Delphi-Quellcode:
///<summary>
/// returns a 16 bit in reversed byte order, e.g. $1234 => $3412)
/// aka converts intel (little endian) to motorola (big endian) byte order format
/// (This is just an alias for system.swap for consistency with Swap32.)
///</summary
function Swap16(_Value: Word): Word;

///<summary>
/// returns a 32 bit value in reversed byte order e.g. $12345678 -> $78563412
/// aka converts intel (little endian) to motorola (big endian) byte order format </summary>
function Swap32(_Value: LongWord): LongWord;
function Swap32pas(_Value: LongWord): LongWord;


function Swap16(_Value: Word): Word;
{$IFDEF SUPPORTS_INLINE}
inline;
{$ENDIF}
begin
  Result := swap(_Value);
end;
// alternative implementation based on https://stackoverflow.com/a/3065619/49925
//function Swap16(Value: smallint): smallint; register;
//asm
//  rol  ax, 8
//end;

function Swap32(_Value: LongWord): LongWord;
asm
  bswap eax
end;

function Swap32pas(_Value: LongWord): LongWord;
begin
  Result := ((_Value shr 24) and $FF) + (((_Value shr 16) and $FF) shl 8) + (((_Value shr 8) and $FF) shl 16) + ((_Value and $FF) shl 24);
end;
(aus meiner dzlib)

Die Konvertierung ist symmetrisch, d.h. auch wenn die Beschreibung sagt Little Endian -> Big Endian, kann man sie auch für Big Endia -> Little Endian verwenden.

Swap32pas ist lediglich eine Pascal-Implementation von Swap32, also ohne Assembler-Code. Das Ergebnis ist identisch.


Alle Zeitangaben in WEZ +1. Es ist jetzt 06:52 Uhr.
Seite 2 von 2     12   

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