在C++中
原型:extern   void   *memcpy(void   *dest,   void   *src,   unsigned   int   count);   
功能:由src所指内存区域复制count个字节到dest所指内存区域。 
由于涉及到内存,不知道c#有对应的方法来替代?高手指点! 
C#中如何将一个数组中的值赋值给一个结构体???结构:
struct retu
        {
            public byte a;//数据头
            public byte b;//下位机地址
            public byte c;//命令
            public char d;//状态
            public float e;//力
            public float f;//电压
            public float g;//电流
            public float h;//运动时间
            public byte i;//数据长度
            public byte Verify;//校验
            public byte j;//数据尾
        }
数组:
Byte[] mbyte = new Byte[11];  

解决方案 »

  1.   


    先取结构指针&retu,然后 Marshal.Copy(mbyte, 0, &ret, 11);是这样用么?
      

  2.   


    和memcpy的方法差不多
    都是用指针
      

  3.   

    Marshal.Copy(mbyte, 0, &ret, 11); 说我有些参数无效,我再试试!
      

  4.   

    Marshal.Copy(IntPtr ptr,byte[] dest,int start,int count);
    Marshal.Copy(&ret, mbyte,0 , 11); &ret要转换成intptr的类型
      

  5.   

    使用这个方法条件没法相等:
     static retu GetRetu( byte[] mbyte )
            {
                if (Marshal.SizeOf(typeof(retu)) == mbyte.Length)              //<-- check their sizes do agree.
                {
                    GCHandle gch = GCHandle.Alloc(mbyte, GCHandleType.Pinned);
                    retu r = (retu)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(retu));
                    gch.Free();                return r;
                }
                else
                {
                    throw new Exception( "no luck, you need to check the manual for the retu struct" );
                }
            }
    我的数组接回来里面只有11个元素:serialPort1.Read(mbyte5, 0, 11);
    用16进制表示就是AA F1 89 1 43 81 26 80 3F 5A C0黑体部分依次是状态,力,电压,电流,时间我的结构定义:
    struct retu
            {
                public byte a;//数据头
                public byte b;//下位机地址
                public byte c;//命令
                public char d;//状态
                public float e;//力
                public float f;//电压
                public float g;//电流
                public float h;//运动时间
                public byte i;//数据长度
                public byte Verify;//校验
                public byte j;//数据尾
            }所以算下来Marshal.SizeOf(typeof(retu))=28,mbyte.Length==11,这样不可能相等!