How do I copy bytes into a struct variable in C#?
问题 I have a struct abc and I want to copy bytes into a struct variable. Similar to memcpy in C/C++. I receive the bytes over socket and they are the bytes of the same struct abc variable. [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct abc { public int a; public int b; public float c; public char[] d; //30 size } 回答1: You can convert the byte array to your structure as follows : int size = Marshal.SizeOf(typeof(abc)); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.Copy(arr, 0,