我现在要向远端的服务器通过SOCKET发送数据 服务端定义了一个标准 就是 4个字节的(int)totalLength + 4个字节的(int)commandId + 4个字节的(byte[])MD5 + 内容()messageBody!    
我现在已经定义了一个对象MESSAGE  他包含了以上字段 
我现在就是不知道怎么把这4段数据放在一个BYTE[]里用来发送个服务端;
我这样写说我错了byte[] bt = { { totallengthArray }, { commandArray }, { this.md5 }, { messageBody } };  
    “错误 无法将类型“byte[]”隐式转换为“byte”
请哪位大虾帮我改下  在线等!谢谢

解决方案 »

  1.   

    你赋值的时候没注意变量的类型。
    变量bt是byte[]型的,不能将byte型赋值给它
      

  2.   

    这方法很多
                System.IO.MemoryStream _Memory = new System.IO.MemoryStream();            int _totalLength=100;
                int _commandId=100;
                byte[] _messageBody=new byte[10];
                _Memory.Write(BitConverter.GetBytes(_totalLength),0,4);
                _Memory.Write(BitConverter.GetBytes(_commandId),0,4);
                _Memory.Write(messageBody, 0, _messageBody.Length);            _Memory.Position = 0;
                byte[] _DataBytes = new byte[_messageBody.Length + 8];
                _Memory.Read(_DataBytes, 0, _DataBytes.Length);使用流来解决2或则 Array.Copy   int _totalLength=100;
                int _commandId=100;
                byte[] _messageBody=new byte[10];
                byte[] _DataBytes = new byte[_messageBody.Length + 8];            Array.Copy(BitConverter.GetBytes(_totalLength), 0, _DataBytes, 0, 4);
                Array.Copy(BitConverter.GetBytes(_commandId), 0, _DataBytes, 4, 4);
                Array.Copy(_messageBody, 0, _DataBytes, 8, 10);
    3 建立结构提
      

  3.   

    可以把消息定义成struct,然后Marshal到byte[]中        public byte[] StructToBytes(object struct)
            {
                int size = Marshal.SizeOf(struct.GetType());
                byte[] bytes = null;            IntPtr buf = Marshal.AllocHGlobal(size);
                try
                {
                    Marshal.StructureToPtr(struct, buf, false);
                    bytes = new byte[size];
                    Marshal.Copy(buf, bytes, 0, size);
                }
                finally
                {
                    Marshal.FreeHGlobal(buf);
                }            return bytes;
            }