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?
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?
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();
}