Code for Encode/Decode QuotedPrintable. [closed]

谁都会走 提交于 2021-02-18 11:55:07

问题


This method Encode your text to QuotedPrintable format

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

This method Decode your text in QuotedPrintable format to string

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;
}

来源:https://stackoverflow.com/questions/11793734/code-for-encode-decode-quotedprintable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!