Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Datentype Array of Byte und Cardinal (https://www.delphipraxis.net/159597-datentype-array-byte-und-cardinal.html)

schwa226 4. Apr 2011 16:22

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:
  TCardinalRec = packed record
    case Integer of
      1 : (_Byte : Array[0..3] of Byte);
      2 : (_Cardinal : Cardinal);
  end;
Nun habe ich aber das Problem, das der Cardinal verkehr rum ist.
Also Byte: 00,00,00,05 => 0x05000000

Eigentlich brauche ich es aber so:
0x00000005

Kann man das irgenwie automatisch umdrehen?

DeddyH 4. Apr 2011 16:37

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?

Uwe Raabe 4. Apr 2011 16:43

AW: Datentype Array of Byte und Cardinal
 
Zitat:

Zitat von DeddyH (Beitrag 1092967)
Ich denke eher, dass Dein Array verkehrt herum ist. Kannst Du es nicht einfach so machen, dass das niederwertigste Byte den Index 0 bekommt?

Delphi-Quellcode:
type
  arr = array[3..0] of Byte;
[DCC Fehler] Unit64.pas(28): E2011 Unterer Bereich überschreitet oberen Bereich

schwa226 4. Apr 2011 16:44

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.

p80286 4. Apr 2011 16:46

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?

DeddyH 4. Apr 2011 16:48

AW: Datentype Array of Byte und Cardinal
 
Zitat:

Zitat von Uwe Raabe (Beitrag 1092969)
Delphi-Quellcode:
type
  arr = array[3..0] of Byte;

So hatte ich das auch nicht gemeint, sondern bei der Datenübergabe.

Uwe Raabe 4. Apr 2011 16:55

AW: Datentype Array of Byte und Cardinal
 
Zitat:

Zitat von schwa226 (Beitrag 1092970)
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.

Dann ist Cardinal nicht der richtige Datentyp! Wenn du weißt, daß der Wert als Big-Endian-Cardinal abgespeichert ist, dann kannst du ihn eben nicht als Little-Endian-Cardinal definieren.

Vielleicht hilft dies ja:

Delphi-Quellcode:
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;
Die beiden Methoden musst du natürlich noch passend implementieren.

schwa226 4. Apr 2011 17:00

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;

himitsu 4. Apr 2011 20:40

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.

gammatester 5. Apr 2011 08:26

AW: Datentype Array of Byte und Cardinal
 
Zitat:

Zitat von schwa226 (Beitrag 1092978)
Fertig und geht:
Delphi-Quellcode:
//...
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;

Das geht aber nur deshalb, weil Du Rangecheck ausgeschaltet hast oder zu kleine Werte nimmst. Immerhin scheinst Du ja nur zu 75% überzeugt zu sein, sonst hättest Du ja auch konsequenterweise _Byte[3] := Value; gesetzt. Also:
Delphi-Quellcode:
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;
wobei man sich bei der _Byte[0]-Berechnung theroretisch das and $FF sparen könnte.


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:09 Uhr.
Seite 1 von 2  1 2      

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