ksmathy
asked on
warning C4311: 'type cast' : pointer truncation
Hi
I have tried to write method which take two 16 bits as input and return on 32 bits number.
I have an warning msg (pointer truncation)in following method.
This warning messge can be ignored or cause the problems
UInt32 getInt32FromInt16(UInt16 MSW,UInt16 LSW)
{
UInt32 *pInt[4];
memcpy(&pInt[0],&LSW,2);
memcpy(&pInt[2],&MSW,2);
return ((UInt32)*pInt);
}
but the below method does not give error msg
UInt64 getInt64FromInt32(UInt32 MSW,UInt32 LSW)
{
UInt64 *pInt[8];
memcpy(&pInt[0],&LSW,4);
memcpy(&pInt[4],&MSW,4);
return ((UInt64)*pInt);
}
is there any other methods which coverts two 16 (or 32)bits into one 32(or 64) bit
In reverse, I also write the method which take large number(32bits) as an input and put it into two 16 bits buffer
void setInt32FromInt16(UInt16& MSW,UInt16& LSW,UInt32 anInteger)
{
char * pCharBuffer = (char*)&anInteger;
UInt16 * pLSW = (UInt16*)&pCharBuffer[0];
UInt16 * pMSW = (UInt16*)&pCharBuffer[2];
MSW=* pMSW ;
LSW=* pLSW ;
}
I just wondering, this method is ok or not
could you please help me out
Thanks
Mat
I have tried to write method which take two 16 bits as input and return on 32 bits number.
I have an warning msg (pointer truncation)in following method.
This warning messge can be ignored or cause the problems
UInt32 getInt32FromInt16(UInt16 MSW,UInt16 LSW)
{
UInt32 *pInt[4];
memcpy(&pInt[0],&LSW,2);
memcpy(&pInt[2],&MSW,2);
return ((UInt32)*pInt);
}
but the below method does not give error msg
UInt64 getInt64FromInt32(UInt32 MSW,UInt32 LSW)
{
UInt64 *pInt[8];
memcpy(&pInt[0],&LSW,4);
memcpy(&pInt[4],&MSW,4);
return ((UInt64)*pInt);
}
is there any other methods which coverts two 16 (or 32)bits into one 32(or 64) bit
In reverse, I also write the method which take large number(32bits) as an input and put it into two 16 bits buffer
void setInt32FromInt16(UInt16& MSW,UInt16& LSW,UInt32 anInteger)
{
char * pCharBuffer = (char*)&anInteger;
UInt16 * pLSW = (UInt16*)&pCharBuffer[0];
UInt16 * pMSW = (UInt16*)&pCharBuffer[2];
MSW=* pMSW ;
LSW=* pLSW ;
}
I just wondering, this method is ok or not
could you please help me out
Thanks
Mat
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I will reply in 1hour.
I'm not at my working place yet.
George Tokas.
I'm not at my working place yet.
George Tokas.
Ok. I'm in place...
It's true I'm getting old...
LSW & MSW had to mean something to me...
ANYWAY....
What you have to do after the trap for more than 0xffff (16bit) and since both MSW & LSW are declared as unsigned int (32bit) is shift left 16 MSW as follows:
unsigned int SMSW;//(Shifted MSW)
SMSW = MSW << 16;
Now add LSW to SMSW and you have what you want...
George Tokas.
It's true I'm getting old...
LSW & MSW had to mean something to me...
ANYWAY....
What you have to do after the trap for more than 0xffff (16bit) and since both MSW & LSW are declared as unsigned int (32bit) is shift left 16 MSW as follows:
unsigned int SMSW;//(Shifted MSW)
SMSW = MSW << 16;
Now add LSW to SMSW and you have what you want...
George Tokas.
Check out the method.
Open a new project and drop at the form 2 TLabels.
At OnCreate add:
unsigned int a,b;
a = 0xa1a1;
b = 0;
Label1->Caption = IntToHex((int)a,8);
b = a << 16;
Label2->Caption = IntToHex((int)b,8);
Label1 will have caption: 0000A1A1.
Label2 will have caption: A1A10000.
That is the MSW get at the upper 16bit nible.
By adding LSW you will have what you want.
George Tokas.
Open a new project and drop at the form 2 TLabels.
At OnCreate add:
unsigned int a,b;
a = 0xa1a1;
b = 0;
Label1->Caption = IntToHex((int)a,8);
b = a << 16;
Label2->Caption = IntToHex((int)b,8);
Label1 will have caption: 0000A1A1.
Label2 will have caption: A1A10000.
That is the MSW get at the upper 16bit nible.
By adding LSW you will have what you want.
George Tokas.
ASKER
I don't think so your solution "return (MSW +LSW) "is right.
Still I try another method
unsigned int CLatAndLongDlg::getOne32Bi
{
char *pChar=(char *)&LSW;
memcpy(&pChar[0],&LSW,2);
memcpy(&pChar[2],&MSW,2);
unsigned int *pInt32=(unsigned int *)&pChar[0];
return *pInt32;
}
could you please it is right method or do you have any other better method
Thanks
Mat