문제

I have an array-

uint8_t arrayTest[] = {
    0, 0xC1,                
    0, 0, 0, 0x0a,          
    0, 0, 0, 0x50           
 };

So the following printf with uint32_t shows the correct output "a":

printf("%x ",ntohl(*((uint32_t *)(arrayTest+2))));

But uint16_t does not show it correctly, although I forward the array pointer two fields, the output is "a0000"-

printf("%x ",ntohl(*((uint16_t *)(arrayTest+4))));

It is same when I use %d:

With printf("%d ",ntohl(*((uint32_t *)(arrayTest+2)))); the output is "10"

With printf("%d ",ntohl(*((uint16_t *)(arrayTest+4)))); the output is "655360"

Why???

도움이 되었습니까?

해결책

nothl is only for converting 32bit quantities. It will produce incorrect result for 16bit quantities, at least on little endian systems. Use ntohsfor that.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top