2017-05-28 4 views
0

Ich versuche, einen guten Weg zu finden, Byte-Array in seine beiden Anfangs diejenigen Decombine:C# Kombinieren/Decombine Zwei-Byte-Arrays

ich die Kombination th zwei Byte-Arrays mit:

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; 
} 

und decombining sie mit:

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 }; 
    } 

aber dieses nicht scheint zu funktionieren, für das erste Array ich erhalte alle notwendigen Bytes es funktioniert perfekt, aber für die seconde Array (byte[] s) Ich verstehe es überhaupt nicht.

Ich habe es versucht, indem ich die Bytes von zwei Dateien kamm file1.txt => containe text = "LM LM LM"; file2.txt => containe text = „hey;

Ich bin der volle Bytes des file1.txt für das erste Array bekommen; aber die file2.txt => Ich bin nur immer: "L" bin ich etwas vermissen zu verstehen? oder etwas fehlt?

dank im Voraus.

Sie bitte die int first zu beachten ist die Länge des ersten Arrays kombiniert

Antwort

0

zuerst

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

entspricht

Array.Copy(array, f, first); 

Zweitens

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

beginnt das Kopieren array vom 0-Index. Was Sie wollen, ist

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