title: C - combine two uint32_t to a double
body: <p>Hi I'm working on socket translation between two protocols. I read from a binary file and store the parsed header into an array of type uint32_t. Then I grab the fields out of the array and convert them into respective types. So far <code>uint32_t</code>/<code>int32_t</code>/<code>uint16_t</code> to <code>int32_t</code> works fine. </p>
  
  <p>However, I get all kinds of wrong outputs when trying to combine two uint32_t (append one after the other) and then converting this 64bit long data into a double.</p>
  
  <p>Being a newbie to C programming, I'm struggling with the computer methodology of <code>double</code> / <code>float</code> representation.</p>
  
  <p>Basically what I want to do is: without altering the bit pattern of the two <code>uint32_t</code>, <strike>concast</strike> concatenate one after the other to make a 64-bit data, then convert the data as a <code>double</code>. The most important thing is not to alter the bit pattern as that part of the bit stream is supposed to be a <code>double</code>.</p>
  
  <p>The following is part of the code:</p>
  
  <pre><code>uint32_t* buffer = (uint32_t*) malloc (arraySize * sizeof(uint32_t));
  
  ...
  
  double outputSampleRate = ((union { double i; double outputSampleRate; })
      { .i = ((uint64_t)buffer[6] &lt;&lt; 32 | (uint64_t)buffer[7])}).outputSampleRate;
  </code></pre>
  
  <p>Data in input file:  </p>
  
  <pre><code>35.5
  </code></pre>
  
  <p>value after my code:</p>
  
  <pre><code>4630192998146113536.000000
  </code></pre>
  
  <p>Also, is there a better way to handle the socket header parsing?</p>