title: Why is this byte to struct de-serialization unit test failing?
body: <p>I have this simple struct:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
  public struct TestStruct
  {
      public bool Valid;     // 1
      public UInt64 Value1;  // 8 
      public UInt64 Value2;  // 8
      public float Value3;   // 4
  }
  </code></pre>
  
  <p>and this generic from byte array to struct method:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static T ByteArrayToStructure&lt;T&gt;(byte[] bytes) where T : struct
  {
      var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
      try 
      {
          return (T) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
      }
      finally 
      {
          handle.Free();
      }
  }
  </code></pre>
  
  <p>and this simple unit test:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[Test]
  public void BytesToMyStruct()
  {
      const bool Valid = true;
      const UInt64 Value1 = 1;
      const UInt64 Value2 = 2;
      const UInt64 Value3 = 99;
  
      var validBytes = BitConverter.GetBytes(Valid);
      var value1Bytes = BitConverter.GetBytes(Value1);
      var value2Bytes = BitConverter.GetBytes(Value2);
      var value3Bytes = BitConverter.GetBytes(Value3);  
  
      // 1 + 8 + 8 + 4 = 21
      var testBytes = new byte[21];
  
      Array.Copy(validBytes, 0, testBytes, 0, 1);
      Array.Copy(value1Bytes, 0, testBytes, 1, 8);
      Array.Copy(value2Bytes, 0, testBytes, 9, 8);
      Array.Copy(value3Bytes, 0, testBytes, 17, 4);
  
      var myStruct = ByteUtils.ByteArrayToStructure&lt;TestStruct&gt;(testBytes);
  
      Assert.That(myStruct.Valid, Is.EqualTo(true));
      Assert.That(myStruct.Value1, Is.EqualTo(1));
      Assert.That(myStruct.Value2, Is.EqualTo(2));
      Assert.That(myStruct.Value3, Is.EqualTo(99));
  }
  </code></pre>
  
  <p>In this unit test I simply wish to manually populate a byte array, deserialize to a struct, and ensure the properties of the struct equal those which were set in the byte array. I assume my approach of manually copying to the same byte array via <code>Array.Copy</code> repeatedly is failing due to the fact that when I execute my unit test, I receive a failure stating that <code>Value1</code> is not being set correctly:</p>
  
  <pre><code>Expected: 1
  But was:  0
  </code></pre>
  
  <p>I am not sure if I am setting my byte arrays correctly by manually setting them for unit testing purposes.</p>