![]() |
Datentype Array of Byte und Cardinal
Hallo,
ich möchte einRecord machen, damit ich auf eine Zahl als Array of Byte und als Ganzzahl zugreifen kann:
Delphi-Quellcode:
Nun habe ich aber das Problem, das der Cardinal verkehr rum ist.
TCardinalRec = packed record
case Integer of 1 : (_Byte : Array[0..3] of Byte); 2 : (_Cardinal : Cardinal); end; Also Byte: 00,00,00,05 => 0x05000000 Eigentlich brauche ich es aber so: 0x00000005 Kann man das irgenwie automatisch umdrehen? |
AW: Datentype Array of Byte und Cardinal
Ich denke eher, dass Dein Array verkehrt herum ist. Kannst Du es nicht einfach so machen, dass das niederwertigste Byte den Index 0 bekommt?
|
AW: Datentype Array of Byte und Cardinal
Zitat:
Delphi-Quellcode:
[DCC Fehler] Unit64.pas(28): E2011 Unterer Bereich überschreitet oberen Bereich
type
arr = array[3..0] of Byte; |
AW: Datentype Array of Byte und Cardinal
Leider nein,
ich muss mich an einen C++ Code halten. Und da ist das Array so aufgebaut. Müsste also irgenwie Little-Endian in Big-Endian wandlen. Also ohne wieder eine extra Funktion dafür zu benötigen. |
AW: Datentype Array of Byte und Cardinal
Das ist bei BigEndian nun mal so.
gib es so aus _Byte[3],Byte[2],_Byte[1],_Byte[0] Gruß K-H Edith: Kein roter Kasten? |
AW: Datentype Array of Byte und Cardinal
Zitat:
|
AW: Datentype Array of Byte und Cardinal
Zitat:
Vielleicht hilft dies ja:
Delphi-Quellcode:
Die beiden Methoden musst du natürlich noch passend implementieren.
type
TCardinalRec = record private function GetCardinal: Cardinal; procedure SetCardinal(const Value: Cardinal); public _Byte : packed Array[0..3] of Byte; property _Cardinal: Cardinal read GetCardinal write SetCardinal; end; |
AW: Datentype Array of Byte und Cardinal
Danke, werde ich machen!
Hatte jetzt versucht mit Swap zu arbeiten - dass gefällt mir aber nicht. Es sollte halt automatisch gehen um Fehler vorzubeugen. Fertig und geht:
Delphi-Quellcode:
TCardinalRec = record
private function GetCardinal : Cardinal; procedure SetCardinal(const Value : Cardinal); public _Byte : packed Array[0..3] of Byte; Property _Cardinal : Cardinal read GetCardinal write SetCardinal; end; function TCardinalRec.GetCardinal : Cardinal; begin Result := (_Byte[0] Shl 24) or (_Byte[1] Shl 16) or (_Byte[2] Shl 8) or _Byte[3]; end; procedure TCardinalRec.SetCardinal(const Value : Cardinal); begin _Byte[0] := Value shr 24; _Byte[1] := Value shr 16; _Byte[2] := Value shr 8; _Byte[3] := Value and $FF; end; |
AW: Datentype Array of Byte und Cardinal
Wobei man auch die das Byte-Array als Property machen könnte und da einfach den Index umdreht.
PS: Das aus der Signatur könnte man auch ganz gut ins DP-Profil eintragen ... hätte beihnah deine Delphiversion übersehn, als ich wissen wollte, ob Recordmethoden möglich sind. |
AW: Datentype Array of Byte und Cardinal
Zitat:
Delphi-Quellcode:
wobei man sich bei der _Byte[0]-Berechnung theroretisch das and $FF sparen könnte.
procedure TCardinalRec.SetCardinal(const Value : Cardinal);
begin _Byte[0] := Value shr 24 and $FF; _Byte[1] := Value shr 16 and $FF; _Byte[2] := Value shr 8 and $FF; _Byte[3] := Value and $FF; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:53 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz