title: C# Converting from byte[] to struct. The byte order is wrong
body: <p>I was trying to use a struct to parse socket data when implement a UDP based protocol.
  And I searched and I can use these 2 functions to convert between byte[] and struct:</p>
  
  <pre><code> byte[] StructToBytes(object structObj)
      {
          int size = Marshal.SizeOf(structObj);
          IntPtr buffer = Marshal.AllocHGlobal(size);
          try
          {
              Marshal.StructureToPtr(structObj, buffer, false);
              byte[] bytes = new byte[size];
              Marshal.Copy(buffer, bytes, 0, size);
              return bytes;
          }
          finally
          {
              Marshal.FreeHGlobal(buffer);
          }
  
      }
  
      static object BytesToStruct(byte[] bytes, Type strcutType, int offset = 0)
      {
          int size = Marshal.SizeOf(strcutType);
          IntPtr buffer = Marshal.AllocHGlobal(size);
          try
          {
              Marshal.Copy(bytes, offset, buffer, size);
              return Marshal.PtrToStructure(buffer, strcutType);
          }
          finally
          {
              Marshal.FreeHGlobal(buffer);
          }
      }
  </code></pre>
  
  <p>Then I had this problem:</p>
  
  <pre><code>//I defined a simple struct with an ushort member
      [StructLayout(LayoutKind.Sequential, Pack = 2)]
      struct S
      {
          public ushort a;
      }
  
      //Then I define a byte[]
      byte[] bArr;
      bArr[0] = 0;
      bArr[1] = 1;
  
      //Convert to struct
      S s = (S)BytesToStruct(bArr, typeof(S));
      //Then s.a = 0x0100 not 0x0001
  </code></pre>
  
  <p>And struct to byte[] is just the same. The 2 bytes of ushort are reversed.
  How do I solve this problem?</p>