Einzelnen Beitrag anzeigen

omata

Registriert seit: 26. Aug 2004
Ort: Nebel auf Amrum
3.154 Beiträge
 
Delphi 7 Enterprise
 
#9

AW: Von C++ nach Delphi (Union + Struct)

  Alt 19. Mai 2011, 08:06
Hier mal mein Vorschlag für D7:

Delphi-Quellcode:
type
  TBMRecContent = (bmRecipient, bmReserved, bmType, bmDir);
  TBMRec = record
    data : Byte;
  end;

  _BM_REQUEST_TYPE = packed record
    case Byte of
      0 : (_BM: TBMRec);
      1 : (B: Byte);
  end;

:

implementation

:

function getBMRecContent(BMRecContent:TBMRecContent; BMRec:TBMRec): Byte;
begin
  case BMRecContent of
    bmRecipient: result := (BMRec.data shr 6) and 3;
    bmReserved: result := (BMRec.data shr 3) and 7;
    bmType: result := (BMRec.data shr 1) and 3;
    bmDir: result := (BMRec.data and 1);
  else
    Result:=0;
  end;
end;

procedure TForm.ButtonClick(Sender: TObject);
var BM_REQUEST_TYPE: _BM_REQUEST_TYPE;
begin
  BM_REQUEST_TYPE._BM.data:=3;
  ShowMessage(Format(
    'Recipient = %d, Reserved = %d, Type = %d, Dir = %d',
    [getBMRecContent(bmRecipient, BM_REQUEST_TYPE._BM),
     getBMRecContent(bmReserved, BM_REQUEST_TYPE._BM),
     getBMRecContent(bmType, BM_REQUEST_TYPE._BM),
     getBMRecContent(bmDir, BM_REQUEST_TYPE._BM)]
  ));
end;

Geändert von omata (19. Mai 2011 um 08:45 Uhr)
  Mit Zitat antworten Zitat