简体   繁体   中英

c# Combine / Decombine Two byte Arrays

I'm trying to find a good way to Decombine byte Array into its two initial ones :

I'm combining th two byte arrays using :

public static  byte[] Combine(params byte[][] arrays)
{
    byte[] rv = new byte[arrays.Sum(a => a.Length)];
    int offset = 0;
    foreach (byte[] array in arrays)
    {
        System.Buffer.BlockCopy(array, 0, rv, offset, array.Length);
        offset += array.Length;
    }
    return rv;
}

and decombining them using :

    public static object[] DeCombine(byte[] array, int first)
    {
        byte[] f = new byte[first];
        byte[] s = new byte[(array.Length - first)];
        Array.Copy(array, f, array.Length - (array.Length - first));
        Array.Copy(array, s, array.Length - first);

        return new[] { f, s };
    }

but this doesnt seems to be working , for the first array i'm getting all the necessary bytes it works perfectly but for the seconde Array ( byte[] s ) I don't get it at all .

i tried it by combing the Bytes of two Files file1.txt = > containe text = "LM LM LM"; file2.txt = > containe text = "hey;

i'm getting for the first array the full bytes of the file1.txt; but the file2.txt = > i'm only getting : "L" am i miss understanding something ? or missing something ?

thanks in advance .

please note the int first is the length of the first array combined

First,

 Array.Copy(array, f, array.Length - (array.Length - first));

is equivalent to

 Array.Copy(array, f, first);

Second,

 Array.Copy(array, s, array.Length - first);

starts copying array from the 0 index. What you want is

 Array.Copy(array,first, s, 0, array.Length - first);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM