简体   繁体   English

c#合并/合并两个字节数组

[英]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 . 但这似乎不起作用,对于第一个数组,我得到了所有必需的字节,它可以正常工作,但是对于第二个数组( byte[] s ),我什么都没得到。

i tried it by combing the Bytes of two Files file1.txt = > containe text = "LM LM LM"; 我通过组合两个文件的字节进行了尝试file1.txt => containse =“ LM LM LM”; file2.txt = > containe text = "hey; file2.txt =>包含文本=“嘿;

i'm getting for the first array the full bytes of the file1.txt; 我正在为第一个数组获取file1.txt的完整字节; but the file2.txt = > i'm only getting : "L" am i miss understanding something ? 但是file2.txt =>我只会得到:“ L”我想念一些东西吗? or missing something ? 还是缺少什​​么?

thanks in advance . 提前致谢 。

please note the int first is the length of the first array combined 请注意, int first是第一个数组组合的长度

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. 从0索引开始复制array What you want is 你想要的是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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