1

thanks to Rudy's Delphi Corner article on C++ -> Delphi conversion I've managed to convert more or less complex structures.

I'd like to ask this community if what I did is correct. Furthermore, I have some doubts to unravel.

This is what I did:

From C++ code:

typedef struct CorrId_t_ { unsigned int size:8; unsigned int valueType:4; unsigned int classId:16; unsigned int reserved:4; unsigned long long intValue; } CorrId_t; 

I did translate to the following code in Delphi (I wrote a few functions to access the bitfield):

interface type CorrId_t_ = record bitfield:Cardinal; //$000000FF=size; $00000F00=valueType; $0FFFF000=classId; $F0000000=reserved; intValue:Cardinal; //should be Uint64 but such integer type doesn't exist in Delphi 6 end; CorrId_t = CorrId_t_; PCorrId_t = ^CorrId_t; procedure CorrID_setSize(var rec:CorrId_t; value:byte); procedure CorrID_setValueType(var rec:CorrId_t; value:byte); procedure CorrID_setClassId(var rec:CorrId_t; value:Word); function CorrID_getSize(rec:CorrId_t):Byte; function CorrID_getValueType(rec:CorrId_t):Byte; function CorrID_getClassId(rec:CorrId_t):Word; implementation procedure CorrID_setSize(var rec:CorrId_t; Value:Byte); begin rec.bitfield := (rec.bitfield and $FFFFFF00) or Value; end; procedure CorrID_setValueType(var rec:CorrId_t; Value:Byte); begin rec.bitfield := (rec.bitfield and $FFFFF0FF) or Value; end; procedure CorrID_setClassId(var rec:CorrId_t; Value:Word); begin rec.bitfield := (rec.bitfield and $F0000FFF) or Value; end; function CorrID_getSize(rec:CorrId_t):Byte; begin Result := rec.bitfield and $000000FF; end; function CorrID_getValueType(rec:CorrId_t):Byte; begin Result := rec.bitfield and $00000F00; end; function CorrID_getClassId(rec:CorrId_t):Word; begin Result := rec.bitfield and $0FFFF000; end; 

Here are my questions for you:

1) are the bitwise operations (inside the set/get access functions) correct?

2) the "unsigned long long" integer type does not have any correspondence in Delphi 6. I can choose between Int64 (but it's signed) or a 32-bit unsigned integer type, such as Cardinal. Which is best in your opinion to use?

Thank you in advance!

2
  • 1
    Use a signed 64 bit integer. The size has to match. As for the bit fields, just test it. Wrote a Delphi program that passes a value to a C++ program and check the right values are passed. Commented Mar 7, 2019 at 14:43
  • 1
    Or, you can use two 32bit Cardinals and do 64bit math manually as needed. It is not unlike various Win32 APIs that expose 64bit integers as separate low and high 32bit integers Commented Mar 7, 2019 at 17:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.