How values gets changed in the type casting from unsigned short to unsigned int
*pSelectData = 4A, *(pSelectData+1) = 54
unsigned int value = ((unsigned开发者_运维问答 short)pSelectData );
Output = 21578 (in hex 0x544A).
Can someone explain me how this is happening (how the values getting converted)??
Thanks in advance
What is the problem more specifically?
Depending on endianness you get either 0x4a54, or 0x544a. That's exactly the representation of your value as it lies in the memory.
This is your memory where p=pSelectedData, ps=cast to short, pint=cast to int (little endian architecture assumed):
[ ][4A][54][00][00][ ]
^ ^ ^ ^ ^
p p+1 p+2 p+3 p+4
ps ps+1 ps+2
pint pint+1
You probably wanted to do this:
*(unsigned short*)pSelectedData = 0x4a;
*(unsigned short*)(pSelectedData+1) = 0x54;
which would give you
[ ][4A][00][54][00][ ]
精彩评论