tags:

views:

26

answers:

3

The value is 10240 or 2800 in hex. TOTAL_LENGTH is a unsigned short. 0028 in decimal is 40 which is what I am expecting (or is at least a reasonable value).

Any ideas why I am getting a 0 instead of a 40? Thinking about reversing the bits myself but really don't want to. xD

unsigned short total_length = ntohl(ipData->TOTAL_LENGTH);

These are the headers I am including.

#include <winsock2.h>
#include <ws2tcpip.h>
+1  A: 
u_long WSAAPI ntohl(
  __in  u_long netlong
);

The result is a long, and you're assigning it to a short. Check if it doesn't get cut.

Also, if it's a short, then why aren't you using ntohs?

Kornel Kisielewicz
Because, I am an idiot. xD Thanks!
bobber205
A: 

try ntohs. It was built for shorts. I'm guessing that it makes a difference since they bothered to make functions for the different types.

Chris H
It makes a difference: `ntohs` can swap bytes 0 and 1, and if you pass in 4 bytes it will discard them, i.e. `3210` becomes `01` `ntohl` swaps byte 0 with 3 and byte 1 with 2, i.e `3210` becomes `0123`
MSalters
A: 

In conjunction to Kornel's answer, you need to be made aware of how the data is stored, the one big mistake is to assume the data is in the way you expect. Different platforms, different processors, the keyword is endianess. Some are stored from High to Low byte, the others are stored from Low to High byte. That is the sole purpose of using 'ntohs' and family. See here for an indepth usage of it by Beej, the network programming site.

Incidentally, you can use this as a standalone function if you were say, processing a data file from a different platform that has a different endianess, you could use this to convert the data to the right endian architecture prior to processing. It does not harm the overhead and guarantee success of processing the data.

Hope this helps, Best regards, Tom.

tommieb75