public byte[] buildBytes(byte order, string data) { byte[] s_part = ToBytes.toBytes(data);//string资料部分 char len = (char)(s_part.Length); byte[] header = BitConverter.GetBytes(len); byte[] o_part = new byte[] { order };//order部分 byte[] final = new byte[s_part.Length + header.Length + o_part.Length]; Console.WriteLine("final len:" + final.Length); header.CopyTo(final, 0); o_part.CopyTo(final, header.Length); s_part.CopyTo(final, o_part.Length + header.Length); return(final); }
public byte[] buildBytes(byte order, int data) { byte[] s_part = ToBytes.toBytes(data);//int资料部分 char len = (char)(s_part.Length); byte[] header = BitConverter.GetBytes(len); //header代表这个封包有多长(不包括header本身),这个存在的原因是防止粘包 Console.WriteLine("header Len:" + header.Length); byte[] o_part = new byte[] { order }; //order部分 byte[] final = new byte[s_part.Length + header.Length + o_part.Length]; Console.WriteLine("final len:" + final.Length); header.CopyTo(final, 0); o_part.CopyTo(final, header.Length); s_part.CopyTo(final, o_part.Length + header.Length); Console.Write("在bulid byte 结束,封包长度为:" + final.Length); return(final); }