Copy Bytes from a byte array to a specific position of another Byte array in C#

给你一囗甜甜゛ 提交于 2019-12-11 07:55:42

问题


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!


回答1:


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

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



回答2:


byte[] array1; array1.CopyTo(array2,pos)



来源:https://stackoverflow.com/questions/26887421/copy-bytes-from-a-byte-array-to-a-specific-position-of-another-byte-array-in-c-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!