字节数组和除外()

时间:2015-02-04 01:39:30

标签: c#

我遇到了Except()C#的问题,我不明白为什么会出现这个问题。 例如,我有数组:[ 4, 6, 8, 9, 1, 3, 5, 9, 3 ]

我使用Except切出:[4,6,8]

[9, 1, 3, 5, 9, 3] - 这应该是结果,但我只得到[ 9, 1 ][3, 5, 9, 3]消失了。

编辑:

有点难以理解,因为我只使用变量。

byte[] bb = new byte[] { 0x46, 0xEE, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4A, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0E, 0x43, 0x68, 0x61, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x20, 0x49, 0x6E, 0x66, 0x6F, 0x31, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0B, 0x47, 0x75, 0x69, 0x6C, 0x64, 0x20, 0x46, 0x6F, 0x72, 0x75, 0x6D, 0x32, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x53, 0x75, 0x70, 0x70, 0x6F, 0x72, 0x74, 0x33, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x4C, 0x65, 0x61, 0x76, 0x65, 0x2D, 0x40, 0x29, 0x0E, 0x02, 0x0F, 0x02, 0x07 };

byte[] torp = new byte[] { 0x46, 0xEE, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4A, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0E, 0x43, 0x68, 0x61, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x20, 0x49, 0x6E, 0x66, 0x6F, 0x31, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0B, 0x47, 0x75, 0x69, 0x6C, 0x64, 0x20, 0x46, 0x6F, 0x72, 0x75, 0x6D, 0x32, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x53, 0x75, 0x70, 0x70, 0x6F, 0x72, 0x74, 0x33, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x4C, 0x65, 0x61, 0x76, 0x65, 0x2D, 0x40 };

byte[] newbb = bb.Except(torp).ToArray();

结果应为:{ 0x29, 0x0E, 0x02, 0x0F, 0x02, 0x07 }但我只获得{ 0x29, 0x0F }

例如,有一个文字:iamdancinginlava。我想删除跳舞。结果应该是:iaminlava。

2 个答案:

答案 0 :(得分:0)

除了不关心物品或重复的顺序。 0x02,0x0f和0x07都显示在torp中,因此它们从结果中删除。

另外,除了使用HashSet<T>执行操作外,如果你有多个0x29或0x0E,你只能得到一个结果。想象一下,除去列表中的所有项目后,输出上都有一个隐式的.Distinct()

答案 1 :(得分:0)

我自己做了些什么。 它看起来很脏,但我用它解决了我的问题。

internal static int[] Locate(this byte[] self, byte[] candidate)
{
    if (IsEmptyLocate(self, candidate))
        return null;

    var list = new List<int>();

    for (int i = 0; i < self.Length; i++)
    {
        if (!IsMatch(self, i, candidate))
            continue;

        list.Add(i);
    }

    return list.Count == 0 ? null : list.ToArray();
}

static bool IsMatch(byte[] array, int position, byte[] candidate)
{
    if (candidate.Length > (array.Length - position))
        return false;

    for (int i = 0; i < candidate.Length; i++)
        if (array[position + i] != candidate[i])
            return false;

    return true;
}

static bool IsEmptyLocate(byte[] array, byte[] candidate)
{
    return array == null
        || candidate == null
        || array.Length == 0
        || candidate.Length == 0
        || candidate.Length > array.Length;
}
internal static byte[] trimArray(byte[] man, byte[] remove)
{
    try
    {
        int seekp = Program.Locate(man, remove)[0];
        if (seekp > 0)
        {
            byte[] pr = new byte[seekp];
            Array.Copy(man, 0, pr, 0, seekp);

            byte[] aa = new byte[man.Length - seekp];

            Array.Copy(man, seekp, aa, 0, man.Length - seekp);
            man = aa;

            byte[] hm = new byte[man.Length - remove.Length];
            Array.Copy(man, seekp + remove.Length, hm, 0, hm.Length);
            return hm;
        }

        byte[] hm = new byte[man.Length - remove.Length];
        Array.Copy(man, seekp + remove.Length, hm, 0, hm.Length);
        return hm;
    }
    catch (Exception ee)
    {
        return new byte[] { };
    }
}