Filter out byte with certain value and XOR next byte

615 Views Asked by At

I receive a byte array and have to remove all 7Dh’s from the array and exclusive or the following byte with 20h in order to recover the original data byte.

What is the best way to do this?

1

There are 1 best solutions below

1
On BEST ANSWER

Well, the first thing to note is that you can't really remove a value from an array, so you can't do it in situ; so perhaps something like:

static byte[] Demungify(byte[] value)
{
    var result = new List<byte>(value.Length);
    bool xor = false;
    for (int i = 0; i < value.Length; i++)
    {
        byte b = value[i];
        if (xor)
        {
            b ^= 0x20;
            xor = false;
        }
        if (b == 0x7D)
        {
            xor = true; // xor the following byte
            continue; // skip this byte
        }
        result.Add(b);
    }
    return result.ToArray();
}