title: C UINT16 How to get it right?
body: <p>I'm new on C programming and I'm testing some code where I receive and process an UDP packet formatted as follow:</p>
  
  <pre><code>UINT16 port1
  UINT16 port2
  </code></pre>
  
  <p>The corresponding values on this test are:</p>
  
  <pre><code>6005
  5555
  </code></pre>
  
  <p>If I print the whole packet buffer I get something like this:</p>
  
  <p><code>u^W³^U&gt;&lt;9e&gt;^D</code></p>
  
  <p>So I thought that I would just have to break it and process as an <code>unsigned int</code> of 16 bytes. So I've tried something like this:</p>
  
  <pre><code>int l = 0;
  unsigned int *primaryPort = *(unsigned int) &amp;buffer[l]; 
  AddToLog(logInfo, "PrimaryPort: %u\n", primaryPort);
  l += sizeof(primaryPort);
  unsigned int *secondaryPort = *(unsigned int) &amp;buffer[l]; 
  AddToLog(logInfo, "SecondaryPort: %u\n", secondaryPort);
  l += sizeof(secondaryPort);
  </code></pre>
  
  <p>But I get wrong numbers with 8 digits.</p>
  
  <p>I even tried another approach like follow, but also get the wrong number as well.</p>
  
  <pre><code>int l = 0;
  unsigned char primaryPort[16]; 
  snprintf(primaryPort, sizeof(primaryPort), "%u", &amp;buffer[l]);
  AddToLog(logInfo, "PrimaryPort: %d\n", primaryPort);
  l += sizeof(primaryPort);
  unsigned char secondaryPort[16]; 
  snprintf(secondaryPort, sizeof(secondaryPort), "%u", &amp;buffer[l]);
  AddToLog(logInfo, "SecondaryPort: %d\n", secondaryPort);
  l += sizeof(secondaryPort);
  </code></pre>
  
  <p>What I'm doing wrong? Also, why I have to free on a char string variables, but I don't need to free on int variables?</p>