QuotedPrintable 编码类可用于vcf解码

本文介绍了一个用于实现 Quoted Printable 编码和解码的 C# 类。该类提供了将字符串转换为 Quoted Printable 格式并进行解码的方法。Quoted Printable 是一种用于电子邮件中非 ASCII 字符编码的方案。

public class QuotedPrintable
{
    private const byte EQUALS = 61;
    private const byte CR = 13;
    private const byte LF = 10;
    private const byte SPACE = 32;
    private const byte TAB = 9;

    /// <summary>
    /// Encodes a string to QuotedPrintable
    /// </summary>
    /// <param name="_ToEncode">String to encode</param>
    /// <returns>QuotedPrintable encoded string</returns>
    public static string Encode(string _ToEncode)
    {
        StringBuilder Encoded = new StringBuilder();
        string hex = string.Empty;
        //byte[] bytes = Encoding.Default.GetBytes(_ToEncode);
        byte[] bytes = Encoding.UTF8.GetBytes(_ToEncode);
        //int count = 0;

        for (int i = 0; i < bytes.Length; i++)
        {
            //these characters must be encoded
            if ((bytes[i] < 33 || bytes[i] > 126 || bytes[i] == EQUALS) && bytes[i] != CR && bytes[i] != LF && bytes[i] != SPACE)
            {
                if (bytes[i].ToString("X").Length < 2)
                {
                    hex = "0" + bytes[i].ToString("X");
                    Encoded.Append("=" + hex);
                }
                else
                {
                    hex = bytes[i].ToString("X");
                    Encoded.Append("=" + hex);
                }
            }
            else
            {
                //check if index out of range
                if ((i + 1) < bytes.Length)
                {
                    //if TAB is at the end of the line - encode it!
                    if ((bytes[i] == TAB && bytes[i + 1] == LF) || (bytes[i] == TAB && bytes[i + 1] == CR))
                    {
                        Encoded.Append("=0" + bytes[i].ToString("X"));
                    }
                    //if SPACE is at the end of the line - encode it!
                    else if ((bytes[i] == SPACE && bytes[i + 1] == LF) || (bytes[i] == SPACE && bytes[i + 1] == CR))
                    {
                        Encoded.Append("=" + bytes[i].ToString("X"));
                    }
                    else
                    {
                        Encoded.Append(System.Convert.ToChar(bytes[i]));
                    }
                }
                else
                {
                    Encoded.Append(System.Convert.ToChar(bytes[i]));
                }
            }
            //if (count == 75)
            //{
            // Encoded.Append("=\r\n"); //insert soft-linebreak
            // count = 0;
            //}
            //count++;
        }

        return Encoded.ToString();
    }

    /// <summary>
    /// Decodes a QuotedPrintable encoded string
    /// </summary>
    /// <param name="_ToDecode">The encoded string to decode</param>
    /// <returns>Decoded string</returns>
    public static string Decode(string _ToDecode)
    {
        //remove soft-linebreaks first
        //_ToDecode = _ToDecode.Replace("=\r\n", "");

        char[] chars = _ToDecode.ToCharArray();

        byte[] bytes = new byte[chars.Length];

        int bytesCount = 0;

        for (int i = 0; i < chars.Length; i++)
        {
            // if encoded character found decode it
            if (chars[i] == '=')
            {
                bytes[bytesCount++] = System.Convert.ToByte(int.Parse(chars[i + 1].ToString() + chars[i + 2].ToString(), System.Globalization.NumberStyles.HexNumber));

                i += 2;
            }
            else
            {
                bytes[bytesCount++] = System.Convert.ToByte(chars[i]);
            }
        }

        //return System.Text.Encoding.Default.GetString(bytes, 0, bytesCount);
        return System.Text.Encoding.UTF8.GetString(bytes, 0, bytesCount);
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值