C UINT16 How to get it right?

前端 未结 3 1256
一整个雨季
一整个雨季 2021-01-16 18:38

I\'m new on C programming and I\'m testing some code where I receive and process an UDP packet formatted as follow:

UINT16 port1
UINT16 port2
3条回答
  •  时光取名叫无心
    2021-01-16 19:32

    You declared primaryPort and secondaryPort to be pointers to unsigned short.

    But when you assign them values from a section of buffer, you already de-referenced the pointer. You don't need pointers-to-unsigned-short. You just need an unsigned short.

    Change it to:

    unsigned short primaryPort = *((unsigned short*) &buffer[l]); 
    
    unsigned short secondaryPort = *((unsigned short *) &buffer[l]); 
    

    Note the removal of a * in the variable declarations.

    If you're still having problems, you'll need to examine buffer byte-by-byte, looking for the value you expect. You can expect that 6005 will show up as either hex 17 75 or 75 17, depending on your platform's endianness.

提交回复
热议问题