Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Byte Array to UInt64 (https://www.delphipraxis.net/159043-byte-array-uint64.html)

schwa226 11. Mär 2011 16:47

Byte Array to UInt64
 
Ich komm einfach nicht drauf.

Ich habe ein Array[0..7] of Byte und will es in ein UInt64 wandeln.
Jedoch kommt immer Blödsinn raus.

Auch dieser Versuch scheitert:
Delphi-Quellcode:
Type
  TUInt64Rec = packed record
    case Integer of
      1 : (_Byte : Array[0..7] of Byte);
      2 : (_UInt64 : UInt64);
  end;
Auch mit einem manuellen SHL schaffe es nicht.
Da sollte ich ja byte[0] um 56 bit, byte[1] um 48 Bit usw shiften.
Das lässt mich Delphi aber nicht. Er erlaubt mir maximal 40 Bit mit SHL.
??

Beipspiel:
Im Byte Array von 0..7:
00 00 00 02 00 0E 8C 19

Ergebins von Delphi: 1840861740884033536

Sollte aber sein: 8590887961

Uwe Raabe 11. Mär 2011 16:54

AW: Byte Array to UInt64
 
Du musst die Reihenfolge der Bytes umdrehen:

Im Byte Array von 0..7:
19 8C 0E 00 02 00 00 00

Auf Intel-Prozessoren steht das niederwertigste Byte vorn (Bitweritgkeit)

schwa226 11. Mär 2011 18:05

AW: Byte Array to UInt64
 
Stimmt! Das spuckt der Windows Rechner auch aus.
Und wie macht man das am besten?

Edit: Habe das gefunden:
Delphi-Quellcode:
procedure SwapUInt64(var X: UInt64);
asm
  mov EBX,dword ptr [X]
  mov ECX,dword ptr [X+4]
  bswap EBX
  bswap ECX
  mov [X],dword ptr ECX
  mov [X+4],dword ptr EBX
end;
Geht und passt jetzt!

Assarbad 11. Mär 2011 20:53

AW: Byte Array to UInt64
 
Wow, danke Uwe. Ich habe bisher noch nicht das deutsche Wort für "Endianess" gekannt. Wieder etwas gelernt :thumb:

schwa226 11. Mär 2011 22:10

AW: Byte Array to UInt64
 
Jetzt habe ich doch noch ein Problem mit der SwapUInt64 festgestellt.
Wenn ich bei der Debug Version das Optimieren ausschalte geht sie.
Wenn ich für das Release das Optimieren einschalte werden andere Speicherbereiche Überschrieben und es kommt zum Absturz.

Gibt es dann dazu noch eine andere Möglichkeit?

himitsu 11. Mär 2011 22:38

AW: Byte Array to UInt64
 
EBX darf niemals verändert werden

Entweder ihr nehmt was Anderers (EAX EDX ECX ESI EDI) oder der Wert von EBX muß gesichert und danach dann wiederhergestellt werden.
PS: EAX ist schon belegt mit X :angle2:

Ich würde auch empfehlen statt X direkt EAX zu nehmen, nicht daß Delphi noch auf die Idee kommt einen Stackframe zu erstellen.

schwa226 12. Mär 2011 08:59

AW: Byte Array to UInt64
 
Delphi-Quellcode:
function SwapUInt64(X: UInt64): UInt64;
asm
  mov EDX,dword ptr [X]
  mov EAX,dword ptr [X+4]
  bswap EDX
  bswap EAX
end;
Dies macht nun keine Probleme mehr :thumb:

schwa226 6. Apr 2011 06:27

AW: Byte Array to UInt64
 
Jetzt nocheinmal dazu:

warum geht das nicht:
Delphi-Quellcode:
function TUInt64Rec2.GetUInt64 : UInt64;
begin
  Result :=
    (_Byte[0] Shl 56) or
    (_Byte[1] Shl 48) or
    (_Byte[2] Shl 40) or
    (_Byte[3] Shl 32) or
    (_Byte[4] Shl 24) or
    (_Byte[5] Shl 16) or
    (_Byte[6] Shl 8) or
    _Byte[7];
end;
Zitat:

[DCC Fehler] E1012 Konstantenausdruck verletzt untere Grenzen
Wie kann man sonst aus 8 Bytes ein 64Bit UInt machen?

Edit:
Es scheint an shl zu liegen. So geht es:
Delphi-Quellcode:
function TUInt64Rec2.GetUInt64 : UInt64;
var
  i1, i2 : UInt64;
begin

  i1 :=
    (_Byte[0] Shl 24) or
    (_Byte[1] Shl 16) or
    (_Byte[2] Shl 8) or
    _Byte[3];

  i2 :=
    (_Byte[4] Shl 24) or
    (_Byte[5] Shl 16) or
    (_Byte[6] Shl 8) or
    _Byte[7];

  Result := (i1 shl 32) or i2;
end;

Uwe Raabe 6. Apr 2011 07:11

AW: Byte Array to UInt64
 
Ich vermute mal, es liegt daran, daß der Compiler kein 64-Bit-Register zur Verfügung hat, in dem er die Shift-Operation durchführen kann.

Laut Hilfe arbeiten die logischen Operatoren auch lediglich auf (32-Bit) Integer.

gammatester 6. Apr 2011 08:20

AW: Byte Array to UInt64
 
Zitat:

Zitat von Uwe Raabe (Beitrag 1093331)
Ich vermute mal, es liegt daran, daß der Compiler kein 64-Bit-Register zur Verfügung hat, in dem er die Shift-Operation durchführen kann.

Laut Hilfe arbeiten die logischen Operatoren auch lediglich auf (32-Bit) Integer.

Die fehlenden 64-Bit-Register sind kein Problem, die Hilfe gibt auch schon den Hinweis. Man muß halt eigentlich nur dafür sorgen, daß Shifts um mehr als 32 Positionen ohne Fehler möglich sind. Das geht, wenn man int64/unit64 shiftet. Also ist ein Typecast erforderlich:
Delphi-Quellcode:
function TUInt64Rec2.GetUInt64 : UInt64;
begin
  Result :=
    (uint64(_Byte[0]) Shl 56) or
    (uint64(_Byte[1]) Shl 48) or
    (uint64(_Byte[2]) Shl 40) or
    (uint64(_Byte[3]) Shl 32) or
    (_Byte[4] Shl 24) or
    (_Byte[5] Shl 16) or
    (_Byte[6] Shl 8) or
    _Byte[7];
end;


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