第一个byte[] byteOne=new byte[]{12,21,33,44,55,66,87,18,29,0,0,0,0,0};
第二个byte[] byteTwo=new byte[]{10,11,12,13,14};
如何把两个byte合为一个,使之变为byte[] bytes=new byte[]{12,21,33,44,55,66,87,18,29,10,11,12,13,14} 寻求好方法...........

解决方案 »

  1.   

      byte[] byteOne = new byte[] { 12, 21, 33, 44, 55, 66, 87, 18, 29, 0, 0, 0, 0, 0 };
                   byte[] byteTwo = new byte[] { 10, 11, 12, 13, 14 };
                 byte[] q=  byteOne.Concat(byteTwo).ToArray();
      

  2.   

    Buffer.BlockCopy方法是将一个数组的字节——不是元素——复制到另一个数组中去
    byte[] bsInput = Encoding.UTF8.GetBytes("");
    int inputBytesCount = bsInput.Length;
    byte[] bs1 = new byte[4 + 3];  
    Buffer.BlockCopy(BitConverter.GetBytes(""), 0, bs1, 0, 4);byte1.CopyTo(newByte, 0);
    byte2.CopyTo(newByte, byte1.length)
    Array.Copy
      

  3.   

                byte[] byteOne = new byte[] { 12, 21, 33, 44, 55, 66, 87, 18, 29, 0, 0, 0, 0, 0 };
                byte[] byteTwo = new byte[] { 10, 11, 12, 13, 14};            int start = Array.IndexOf(byteOne, (byte)0);
                Array.Copy(byteTwo, 0, byteOne,start , (byteTwo.Length > byteOne.Length - start? byteOne.Length - start: byteTwo.Length) );
      

  4.   

    不行,接收服务器消息是这样:ClientConnService.localSocket.Receive(bufData, 2, 0);
                            int len0 = bufData[0] * 1;
                            int len1 = bufData[1] * 255;
                            int len = len0 + len1;
                            byteGather = new byte[len];
                            while (len > 0)
                            {
                                bufData = new byte[len];
                                int recIndex = ClientConnService.localSocket.Receive(bufData, len, 0);
                                len = len - recIndex;
                            }其中len为接收数据的长度,有的时候数据过大一次不能接收完,要分几次接收,在这里我用了一个while,服务器发送的数据实际长度是50,客户端计算出要接收的数据len=50,而在接收数据时,由于各种原因只能够接收到40位的数据,后面还有10位数据没有接收,那么得到第一个bufData数据的实际长度是40,但是bufData前面已经付给了长度即为len,也就是说bufData后面的10个长度是空的.第二次接收数据的时候len=10,那么接收到的这10位长度数据,我如何添加到bufData的后面10位为空的位置中,如果数据很大while循环可能不止两次,如何解决......
      

  5.   

                foreach (byte b in byteOne)
                    Console.WriteLine(b);
    //输出
      

  6.   

    那你用List<byte>方便些。byteList.AddRange( arrayTwo );
    ...
    byte[] result = byteList.ToArray();
      

  7.   

    这样也可以        public byte[] byteAdd(byte[] pre, byte[] data)
            {
                byte[] tempdata = new byte[ pre.Length + data.Length];
                Array.Copy(pre, 0, tempdata, 0, pre.Length);
                Array.Copy(data, 0, tempdata, pre.Length, data.Length);
                return tempdata;
            }
      

  8.   

    我要的方法是byte中不能存在空的占位符,四楼说的那个是知道是0的情况下才能做到.
      

  9.   


     byte[] byteOne = new byte[] { 12, 21, 33, 44, 55, 66, 87, 18, 29, 0, 0, 0, 0, 0 };
                    byte[] byteTwo = new byte[] { 10, 11, 12, 13, 14 };
                    byte[] q = byteOne.Concat(byteTwo).Where(a=>(int)a>0).ToArray();
      

  10.   

    我以前用写到stream里,然后再读出来。
      

  11.   

    为什么都写这么复杂?new List<byte>(byteOne).AddRange(byteTwo).ToArray();不就行了吗
      

  12.   

    转换型string,加一起,再转换回byte[]。