Я реализовал кодировку для байтов. Надеюсь, это поможет.
public byte[] Encode(byte[] original)
{
// TODO: Write your encoder here
if (original==null || original.Count() == 0) // Check for invalid inputs
return new byte[0];
var encodedBytes = new List<byte>(); // Byte list to be returned
byte run = 0x01;
for (int i = 1; i < original.Length; i++)
{
if (original[i] == original[i - 1]) // Keep counting the occurences till this condition is true
run++;
else // Once false,
{
encodedBytes.Add(run); // add the total occurences followed by the
encodedBytes.Add(original[i - 1]); // actual element to the Byte List
run = 0x01; // Reset the Occurence Counter
}
if (i == original.Length - 1)
{
encodedBytes.Add(run);
encodedBytes.Add(original[i]);
}
}
return encodedBytes.Count()==0 ? new byte[0] : encodedBytes.ToArray<byte>();
}
var a = new byte[]{0x01, 0x02, 0x03, 0x04};
var b = new byte[]{0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04};
var EncodedA = Encode(a);
var isAEqualB = EncodedA.SequenceEqual(b); should return true