C# Quoted Printable

Enjoy!

static string DecodeQuotedPrintables(string input)  
    {
        var occurences = new Regex("(\\=[0-9A-F][0-9A-F])+");
        var matches = occurences.Matches(input);

        foreach (Match match in matches)
        {
            byte[] bytes = new byte[match.Value.Length / 3];
            for (int i = 0; i < match.Value.Length / 3; i ++)
            {
                bytes[i] = byte.Parse(match.Value.Substring(i * 3 + 1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            }
            char[] hexChar = Encoding.UTF8.GetChars(bytes);
            input = input.Replace(match.Value, hexChar[0].ToString());
        }
        return input;
    }

    static string DecodeQuotedPrintables(string input)
    {
        var result = new StringBuilder();
        var byteBuffer = new Queue<byte>();
        var i = 0;

        while(i < input.Length)
        {
            char ch = input[i];
            if (ch == '=' && i < input.Length - 2)
            {
                char ch1 = input[i + 1];
                char ch2 = input[i + 2];
                if (IsHex(ch1) && IsHex(ch2))
                {
                    var temp = (byte.Parse(new string(new char[] { ch1, ch2 }), NumberStyles.AllowHexSpecifier));
                    byteBuffer.Enqueue(temp);
                    i += 3;
                    continue;
                }
            }

            if (byteBuffer.Count > 0)
            {
                var tempBuffer = new List<byte>();
                while((byte b = byteBuffer.Dequeue()) != 0x00)
                {
                    char[] hexChar = Encoding.UTF8.GetChars(te.ToArray());
                    result.Append(hexChar[0].ToString());
                }


            }

            result.Append(ch);
            i += 1;
        }
        return result.ToString();
comments powered by Disqus