Run-Length Encoding (RLE) in C#

I recently came across a file format that required usage of Run Length Encoding to handle. This involved writing methods for both decoding and encoding. Here are my results:

To decode, you just use a string and concatenate it with each numeric character that is encountered until you hit a non-numeric character. You use this string to figure out how many times you need to repeat the character:

public static string Decode(string toDecode)
{
    string coefficient = String.Empty;
    StringBuilder sb = new StringBuilder();

    foreach (char current in toDecode)
    {
        if (char.IsDigit(current))
            coefficient += current;
        else
        {
            if (coefficient == String.Empty)
                sb.Append(current);
            else
            {
                int count = int.Parse(coefficient);
                coefficient = String.Empty;
                for (int j = 0; j < count; j++)
                    sb.Append(current);
            }
        }
    }
    return sb.ToString();
}

Similarly, for encoding. I used a stringbuilder and a counter to figure out how many times a character was repeated then appended this to the stringbuilder:

public static string Encode(string toEncode)
{
    StringBuilder sb = new StringBuilder();
    int count = 1;
    char current = toEncode[0];
    for (int i = 1; i < toEncode.Length; i++)
    {
        if (current == toEncode[i])
        {
            count++;
        } else
        {
            sb.AppendFormat("{0}{1}", count, current);
            count = 1;
            current = toEncode[i];
        }
    }
    sb.AppendFormat("{0}{1}", count, current);
    return sb.ToString();
}

Note that RLE is a primitive form of compression and should probably be avoided. If you need to compress something for transmission or storage you might be better off using something like GZipStream.

Did this post help you? Buy me a coffee!
Buy Me A Coffee
comments powered by Disqus