Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just use this two methods:</p> <pre><code>public static class StructTools { /// &lt;summary&gt; /// converts byte[] to struct /// &lt;/summary&gt; public static T RawDeserialize&lt;T&gt;(byte[] rawData, int position) { int rawsize = Marshal.SizeOf(typeof(T)); if (rawsize &gt; rawData.Length - position) throw new ArgumentException("Not enough data to fill struct. Array length from position: "+(rawData.Length-position) + ", Struct length: "+rawsize); IntPtr buffer = Marshal.AllocHGlobal(rawsize); Marshal.Copy(rawData, position, buffer, rawsize); T retobj = (T)Marshal.PtrToStructure(buffer, typeof(T)); Marshal.FreeHGlobal(buffer); return retobj; } /// &lt;summary&gt; /// converts a struct to byte[] /// &lt;/summary&gt; public static byte[] RawSerialize(object anything) { int rawSize = Marshal.SizeOf(anything); IntPtr buffer = Marshal.AllocHGlobal(rawSize); Marshal.StructureToPtr(anything, buffer, false); byte[] rawDatas = new byte[rawSize]; Marshal.Copy(buffer, rawDatas, 0, rawSize); Marshal.FreeHGlobal(buffer); return rawDatas; } } </code></pre> <p>And specify your struct like this (Specify the exact size and pack (align) by one byte. default is 8):</p> <pre><code>[StructLayout(LayoutKind.Explicit, Size = 11, Pack = 1)] private struct MyStructType { [FieldOffset(0)] public UInt16 Type; [FieldOffset(2)] public Byte DeviceNumber; [FieldOffset(3)] public UInt32 TableVersion; [FieldOffset(7)] public UInt32 SerialNumber; } </code></pre> <p>Now you can Deserialize using</p> <pre><code>StructTools.RawDeserialize&lt;MyStructType&gt;(byteArray, 0); // 0 is offset in byte[] </code></pre> <p>and serialize using</p> <pre><code>StructTools.RawSerialize(myStruct); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload