title: Serialize double and float with C
body: <p>How can I serialize doubles and floats in C?</p>
  
  <p>I have the following code for serializing shorts, ints, and chars.</p>
  
  <pre><code>unsigned char * serialize_char(unsigned char *buffer, char value)
  {
      buffer[0] = value;
      return buffer + 1;
  }
  
  unsigned char * serialize_int(unsigned char *buffer, int value)
  {
      buffer[0] = value &gt;&gt; 24;
      buffer[1] = value &gt;&gt; 16;
      buffer[2] = value &gt;&gt; 8;
      buffer[3] = value;
      return buffer + 4;
  }
  
  unsigned char * serialize_short(unsigned char *buffer, short value)
  {
      buffer[0] = value &gt;&gt; 8;
      buffer[1] = value;
      return buffer + 2;
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>I found these functions from <a href="https://stackoverflow.com/questions/1577161/passing-a-structure-through-sockets-in-c">this question</a></p>
  
  <p>Edit 2:</p>
  
  <p>The purpose of serializing is to send data to a UDP socket and guarantee that it can be deserialized on the other machine even if the endianness is different. Are there any other "best practices" to perform this functionality given that I have to serialize ints, doubles, floats, and char*?</p>