Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm unsure of exactly what you are asking. Are you trying to get an equivalent structure definition in C# for plain old C# usage or for interop (PInvoke) purposes? If it's for PInvoke the follownig structure will work</p> <pre><code>[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct tPacket { /// WORD-&gt;unsigned short public ushort word1; /// WORD-&gt;unsigned short public ushort word2; /// BYTE-&gt;unsigned char public byte byte1; /// BYTE-&gt;unsigned char public byte byte2; /// BYTE[8] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=8, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)] public byte[] array123; } </code></pre> <p>If you are looking for a plain old C# structure that has the same characteristics, it's unfortunately not possible to do with a struct. You cannot define an inline array of a contstant size in a C# structure nor can you force the array to be a specific size through an initializer. </p> <p>There are two alternative options in the managed world.</p> <p>Use a struct which has a create method that fills out the array </p> <pre><code>[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct tPacket { public ushort word1; public ushort word2; public byte byte1; public byte byte2; public byte[] array123; public static tPacket Create() { return new tPacket() { array123 = new byte[8] }; } } </code></pre> <p>Or alternatively use a class where you can initialize the array123 member variable directly.</p> <p><strong>EDIT</strong> OP watns to know how to convert a byte[] into a tPacket value</p> <p>Unfortunately there is no great way to do this in C#. C++ was awesome for this kind of task because has a very weak type system in that you could choose to view a stream of bytes as a particular structure (evil pointer casting). </p> <p>This may be possible in C# unsafe code but I do not believe it is. </p> <p>Essentially what you will have to do is manually parse out the bytes and assign them to the various values in the struct. Or write a native method which does the C style casting and PInvoke into that function. </p>
    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.
 

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