开发者

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][  ]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜