beefeater267
Programmer
- Apr 6, 2005
- 79
Hi,
I'm new to C++ and am trying to use BITFIELDS. Here's the code i have thus far:
typedef unsigned int BITFIELD;
typedef struct SelfRegistrationMsg_T
{
//byte 5
BITFIELD rackID : 8;
//byte 6
BITFIELD controllerID : 8;
//byte 7 / 8
BITFIELD controllerNetmask : 8;
} SelfRegistrationMsg;
union JOTRONCmd_T
{
byte *payload;
SelfRegistrationMsg_T selfRegMsg;
};
class CJOTRON
{
public:
CJOTRON::CJOTRON();
virtual CJOTRON::~CJOTRON();
JOTRONCmd_T cmd;
bool BuildCmd(const BYTE payload[]);
private:
};
All above is in a .h file..... and in in the cooresponding .cpp file i have:
bool CJOTRON::BuildCmd(const BYTE payload[])
{
bool bReturn = false;
int payloadSize = ((sizeof(payload) / sizeof(byte)) - 1);
this->cmd.payload = new byte[payloadSize];
memcpy(&this->cmd.payload[0],&payload[0],payloadSize);
bReturn = true;
return bReturn;
}
So, i'm trying to use the BITFIELD in a test.cpp class, with code such as:
CJOTRON* jotCmd = new CJOTRON();
byte* payload;
payload = new byte[3];
memset(payload, 0, (sizeof(byte) * 3));
payload[0] = 0x02;
payload[1] = 0x03;
payload[2] = 0x04;
jotCmd->BuildCmd(payload);
unsigned int a = jotCmd->cmd.selfRegMsg.rackID;
unsigned int b = jotCmd->cmd.selfRegMsg.controllerID;
unsigned int c = jotCmd->cmd.selfRegMsg.controllerNetmask;
//**************************************
So, in my test, all i'm doing is just setting 3 values of an array and copying it into my object with BuildCmd, and just testing to read them back with my bitField struct.
however, instead of getting what i expect:
(rackID = 0x02, controllerID = 0x03, controllerNetmask = 0x04), i get back other values.
Any idea what i may be doing wrong?
I'm new to C++ and am trying to use BITFIELDS. Here's the code i have thus far:
typedef unsigned int BITFIELD;
typedef struct SelfRegistrationMsg_T
{
//byte 5
BITFIELD rackID : 8;
//byte 6
BITFIELD controllerID : 8;
//byte 7 / 8
BITFIELD controllerNetmask : 8;
} SelfRegistrationMsg;
union JOTRONCmd_T
{
byte *payload;
SelfRegistrationMsg_T selfRegMsg;
};
class CJOTRON
{
public:
CJOTRON::CJOTRON();
virtual CJOTRON::~CJOTRON();
JOTRONCmd_T cmd;
bool BuildCmd(const BYTE payload[]);
private:
};
All above is in a .h file..... and in in the cooresponding .cpp file i have:
bool CJOTRON::BuildCmd(const BYTE payload[])
{
bool bReturn = false;
int payloadSize = ((sizeof(payload) / sizeof(byte)) - 1);
this->cmd.payload = new byte[payloadSize];
memcpy(&this->cmd.payload[0],&payload[0],payloadSize);
bReturn = true;
return bReturn;
}
So, i'm trying to use the BITFIELD in a test.cpp class, with code such as:
CJOTRON* jotCmd = new CJOTRON();
byte* payload;
payload = new byte[3];
memset(payload, 0, (sizeof(byte) * 3));
payload[0] = 0x02;
payload[1] = 0x03;
payload[2] = 0x04;
jotCmd->BuildCmd(payload);
unsigned int a = jotCmd->cmd.selfRegMsg.rackID;
unsigned int b = jotCmd->cmd.selfRegMsg.controllerID;
unsigned int c = jotCmd->cmd.selfRegMsg.controllerNetmask;
//**************************************
So, in my test, all i'm doing is just setting 3 values of an array and copying it into my object with BuildCmd, and just testing to read them back with my bitField struct.
however, instead of getting what i expect:
(rackID = 0x02, controllerID = 0x03, controllerNetmask = 0x04), i get back other values.
Any idea what i may be doing wrong?