Einzelnen Beitrag anzeigen

Benutzerbild von dummzeuch
dummzeuch

Registriert seit: 11. Aug 2012
Ort: Essen
1.468 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#12

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

  Alt 25. Feb 2020, 14:19
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.
Thomas Mueller

Geändert von dummzeuch (25. Feb 2020 um 14:22 Uhr)
  Mit Zitat antworten Zitat