C UINT16 How to get it right?

前端 未结 3 1270
一整个雨季
一整个雨季 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:43

    unsigned int on your system is likely 4 bytes (uint32_t). You can use unsigned int here if you mask out the values in the correct endianess, or simply use a short.

    int l = 0;
    unsigned short *primaryPort = *(unsigned short) &buffer[l]; 
    AddToLog(logInfo, "PrimaryPort: %u\n", primaryPort);
    l += sizeof(*primaryPort);
    unsigned short *secondaryPort = *(unsigned short) &buffer[l]; 
    AddToLog(logInfo, "SecondaryPort: %u\n", secondaryPort);
    l += sizeof(*secondaryPort);
    

提交回复
热议问题