title: Serialize a struct and send it via socket with C++
body: <p>i would send a struct via C++ sockets. This is an example struct:</p>
  
  <pre><code>struct PIPPO
  {
  int x;
  int y;
  };
  </code></pre>
  
  <p>that i use with:</p>
  
  <pre><code>PIPPO test2;
  test2.x = 10;
  test2.y = 20;
  </code></pre>
  
  <p>and I have the code above to serialize and send it via socket. The problem is that if I try to get the HEX value of the test variable i only see 0A and infact on the other computer that receives the data I cannot convert back the binary data into a struct. Can someone help me?</p>
  
  <pre><code>template &lt;class T&gt; void SerializeData(char *outputArray, T inputData)
  {
  memcpy(outputArray, &amp;inputData, sizeof(inputData));
  }
  
  char *StrToHexStr(char *str)
  {
      char *newstr = new char[(strlen(str)*2)+1];
      char *cpold = str;
      char *cpnew = newstr;
  
      while('\0' != *cpold) {
          sprintf(cpnew, "%02X", (char)(*cpold++));
          cpnew+=2;
      }
      *(cpnew) = '\0';
      return(newstr);
  }
  
  char *test = new char[sizeof(PIPPO)];
  
  memcpy((void *)&amp;test, (void *)&amp;test2, sizeof(test2));
  
  send(this-&gt;m_socket, test, strlen(test), 0);
  </code></pre>