title: Type coercion in c: unsigned int to float
body: <p>I'm communicating serially between a host pc and an embedded processor.  On the embedded side, I need to parse character strings for floating point and integer data.  What I am currently doing is something along these lines:</p>
  
  <pre><code>inline float32* fp_unpack(float32* dest, volatile char* str) {
      Uint32 temp = (Uint32)str[3]&lt;&lt;24;
      temp |= (Uint32)str[2]&lt;&lt;16;
      temp |= (Uint32)str[1]&lt;&lt;8;
      temp |= (Uint32)str[0];
      temp = (float32)temp;
      *dest = (float32)temp;
  
      return dest;
  }
  </code></pre>
  
  <p>Where str has four characters, each representing a byte of the float.  The bytes in string are ordered little endian.</p>
  
  <p>As an example, I'm trying to extract the number 100.0 from str.  I've verified the contents of string are:</p>
  
  <p>s[0]: 0x00,
  s[1]: 0x00,
  s[2]: 0x20,
  s[3]: 0x41,</p>
  
  <p>which is the 32 bit floating point representation of 100.0.  Furthermore, I've verified that the function successfully sets temp to 0x41200000.  However, dest ends up being 0x4e824000.  I know the problem arises from the line: *dest = (float32)temp, which I hoped would simply copy the bits from temp to dest, with a typecast to make the compiler happy.</p>
  
  <p>However, I've realized that this won't be the case, since the operation: float x = (float)4/3 actually converts 4 to 4.0, ie changing the bits.</p>
  
  <p>How do I coerce the bits in temp into dest?</p>
  
  <p>Thanks in advance</p>
  
  <p>edit: Note that 0x4120000 as an integer is 1092616192, which, as a float, is 0x4e82400</p>