从字节数组到另一个字节数组在C#中的特定位置复制字节 [英] Copy Bytes from a byte array to a specific position of another Byte array in C#

查看:1350
本文介绍了从字节数组到另一个字节数组在C#中的特定位置复制字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字节数组 - 数组1和数组2。
我的目标是字节从第1个数组第二方面复制到每个阵列的开始索引,并填补了非填充字节与特定字节。

 字节[]数组1 =新的字节[5]
数据如下:11,22,00,33,44;字节[]数组2 =新的字节[10];

我需要的字节数在array1复制到ARRAY2。这些数据需要从位置3复制数组2和值FF填补了空位置的其余部分。
即我的结果数组2将{FF,FF,FF,11,22,00,33,44,FF,FF}

任何帮助将是AP preciable。

在此先感谢!


解决方案

  //初始化数组2至0xFF
的for(int i = 0; I< array2.Length;我++)
    数组2 [I] = 0xFF的;//复制
Array.Copy(数组1,0,数组2,3,array2.Length);

I have two byte arrays - array1 and array2 . My aim is to copy the bytes from 1st array to second with respect to the start index of each array and fill the non filled bytes with a specific byte.

byte[] array1 = new byte[5]
The data is as follows: 11,22,00,33,44; 

byte[] array2 = new byte[10];     

I need to copy the bytes from array1 to array2. The data needs to be copied from position 3 in array2 and fill the rest of the empty positions with value ff. ie my result in array2 would be {ff,ff,ff,11,22,00,33,44,ff,ff}

Any help would be appreciable.

Thanks in advance!

解决方案

// Init array2 to 0xff
for (int i = 0; i < array2.Length; i++)
    array2[i] = 0xff;

// Copy
Array.Copy(array1, 0, array2, 3, array2.Length);

这篇关于从字节数组到另一个字节数组在C#中的特定位置复制字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆