文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Decodes a QuotedPrintable encoded string

Decodes a QuotedPrintable encoded string

时间:2010-12-23  来源:skyfei

 ENCODING=QUOTED-PRINTABLE:=E9=A3=9E;=E5=88=98;=E7=A7=8D;=E5=85=88=E7=94=9F;=E5=8F=B7

while in the outlook vcard, it will like:

ENCODING=QUOTED-PRINTABLE:=E5=95=8A=E5=95=8A

 

that mean that the android will not encode the normail char

so update one decode method, which I find in the internet:

 

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

            Encoding encoding;
            if (string.IsNullOrEmpty(encoderName))
            {
                encoding = Encoding.Default;
            }
            else
            {
                try
                {
                    encoding = Encoding.GetEncoding(encoderName);
                   // encoding = Encoding.Default;
                }
                catch
                {
                    encoding = Encoding.Default;
                }
            }

            char[] chars = _ToDecode.ToCharArray();

        //    byte[] bytes = new byte[chars.Length];
            List<byte> bytes = new List<byte>();
            int bytesCount = 0;

            StringBuilder sb = new StringBuilder();

            string Hex = string.Empty;
            bool flag = false;
            foreach (char c in chars)
            {
                if (c == '=')
                {
                    flag = true;
                }
                else
                {
                    if (flag)
                    {
                        Hex += c;
                        if (Hex.Length == 2)
                        {
                            bytes.Add(System.Convert.ToByte(int.Parse(Hex, System.Globalization.NumberStyles.HexNumber)));
                            Hex = string.Empty;
                            flag = false;
                        }
                    }
                    else
                    {
                        if (bytes.Count >0)
                        {
                            sb.Append( encoding.GetString(bytes.ToArray(), 0, bytes.Count));
                            bytes.Clear();
                        }
                        sb.Append(c);
                    }
                }
            }

            if (bytes.Count > 0)
            {
                sb.Append(encoding.GetString(bytes.ToArray(), 0, bytes.Count));
                bytes.Clear();
            }

            // original code
            //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 encoding.GetString(bytes, 0, bytesCount);
            return sb.ToString();
        }

 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载