typedef struct {
int iLen;
BYTE bType;
char cTable;
BYTE* pMsg;
}Packet,*pPacket;
void CStructTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
Packet m_Packet;
m_Packet.iLen =
12;
m_Packet.bType =
0x01;
m_Packet.cTable =
'D';
m_Packet.pMsg =
new BYTE[10];
memset(m_Packet.pMsg,0x00,10);
CString strMsg =
"1234567890";
memcpy(m_Packet.pMsg,strMsg.GetBuffer(strMsg.GetLength()),strMsg.GetLength());
BYTE bBuffer[20] = {0x00};
memcpy(bBuffer,&m_Packet,16);
CString strTemp;
CString strStruct =
"";
for(int i=0;i<16;i++)
{
strTemp.Format("%02X ",bBuffer);
strStruct += strTemp;
}
AfxMessageBox(strStruct);
}
结构体变量中的整型变量iLen按字节打印出来为什么是高低位反过来?
m_Packet.iLen = 12; 应该m_Packet.iLen = 0x00 00 00 0C。但是通过给打印出来为0C 00 00 00。什么原因各位给个解释以及解决方案?
另外按照以上代码打印出来,结构体的指针变量pMsg的内容打印也是错误,是不是结构体中只是保存了该指针的地址?按如下方法可以准确打印出指针变量的内容。
void CStructTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
Packet m_Packet;
m_Packet.iLen =
12;
m_Packet.bType =
0x01;
m_Packet.cTable =
'D';
m_Packet.pMsg =
new BYTE[10];
memset(m_Packet.pMsg,0x00,10);
CString strMsg =
"1234567890";
memcpy(m_Packet.pMsg,strMsg.GetBuffer(strMsg.GetLength()),strMsg.GetLength());
BYTE bBuffer[20] = {0x00};
memcpy(bBuffer,&m_Packet,16);
CString strTemp;
CString strStruct =
"";
for(int i=0;i<6;i++)
{
strTemp.Format("%02X ",bBuffer);
strStruct += strTemp;
}
for(int j=0;j<10;j++)
{
strTemp.Format("%02X ",*(m_Packet.pMsg+j));
strStruct += strTemp;
}
AfxMessageBox(strStruct);
}
|