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
The corresponding values on this test are:
6005
5555
If I print the whole packet buffer I get something like this:
u^W³^U><9e>^D
How can I break it and get the right values? I've tried uint16_t
and unsigned short
and even the function htons
, but didn't work either I just get wrong numbers, I'm trying something like this:
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);
What is the right or the best way to get these values? I think these two uint16 values are not integer formatted, I think it's just an uint16
char text on this characters u^W³^U><9e>^D
, so perhaps I'm getting the wrong numbers because I'm processing like an integer. But I've tried everything and I just can't get the right values.