Код для кодирования/декодирования QuotedPrintable.

Этот метод кодирует ваш текст в формат QuotedPrintable

public static string EncodeQuotedPrintable(string value)
{
    if (string.IsNullOrEmpty(value))
        return value;

    StringBuilder builder = new StringBuilder();

    byte[] bytes = Encoding.UTF8.GetBytes(value);
    foreach (byte v in bytes)
    {
        // The following are not required to be encoded:
        // - Tab (ASCII 9)
        // - Space (ASCII 32)
        // - Characters 33 to 126, except for the equal sign (61).

        if ((v == 9) || ((v >= 32) && (v <= 60)) || ((v >= 62) && (v <= 126)))
        {
            builder.Append(Convert.ToChar(v));
        }
        else
        {
            builder.Append('=');
            builder.Append(v.ToString("X2"));
        }
    }

    char lastChar = builder[builder.Length - 1];
    if (char.IsWhiteSpace(lastChar))
    {
        builder.Remove(builder.Length - 1, 1);
        builder.Append('=');
        builder.Append(((int)lastChar).ToString("X2"));
    }

    return builder.ToString();
}

Этот метод декодирует ваш текст в формате QuotedPrintable в строку

public static string DecodeQuotedPrintable(string input, string charSet)
{
    Encoding enc;

    try
    {
        enc = Encoding.GetEncoding(charSet);
    }
    catch
    {
        enc = new UTF8Encoding();
    }

    var occurences = new Regex(@"(=[0-9A-Z]{2}){1,}", RegexOptions.Multiline);
    var matches = occurences.Matches(input);

    foreach (Match match in matches)
    {
        try
        {
            byte[] b = new byte[match.Groups[0].Value.Length / 3];
            for (int i = 0; i < match.Groups[0].Value.Length / 3; i++)
            {
                b[i] = byte.Parse(match.Groups[0].Value.Substring(i * 3 + 1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            }
            char[] hexChar = enc.GetChars(b);
            input = input.Replace(match.Groups[0].Value, new String(hexChar));
        }
        catch
        { ;}
    }
    input = input.Replace("?=", "").Replace("\r\n", "");

    return input;
}

person Pavel Gr.    schedule 03.08.2012    source источник
comment
было бы неплохо указать язык в тегах или в строке темы (даже если это кажется очевидным после прочтения кода)   -  person lajarre    schedule 05.10.2012
comment
Было бы неплохо иметь вопрос в вопросе?   -  person Joe    schedule 10.06.2013
comment
Хороший пример кода в любом случае. Я нашел это полезным :)   -  person Paul    schedule 02.09.2015
comment
В примере кодирования, похоже, отсутствуют мягкие разрывы строк для длинных строк.   -  person jk7    schedule 23.11.2016