摘要:
下文通过c#将字节数组合并的方法说明,如下所示;
实现思路: 1.定义一个新的字节数组, 其数组长度为两个数组之和 2.使用数组的Array.Copy方法将字节数组分别复制到新的字节数组中
例:
将两个字节数组合并到一个新的字节数组
using System; using System.Text; using System.Collections.Generic; namespace consoleTest { class Program { static void Main(string[] args) { Console.WriteLine("maomao365.com示例分享"); string s1 = "maomao365.com"; byte[] bytes1 = Encoding.UTF8.GetBytes(s1); string s2 = "linux28.com"; byte[] bytes2 = Encoding.UTF8.GetBytes(s2); /*合并字节数组的方法*/ byte[] byteAll = new byte[bytes1.Length + bytes2.Length]; Array.Copy(bytes1, 0, byteAll, 0, bytes1.Length); Array.Copy(bytes2, 0, byteAll,bytes1.Length, bytes2.Length); Console.WriteLine("合并后的字节数组"); Console.WriteLine(Encoding.UTF8.GetString(byteAll)); Console.ReadLine(); } } }