zoukankan      html  css  js  c++  java
  • Decodes a QuotedPrintable encoded string

    In android vcard file, the QuotedPrintable string will be like:

     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();
            }
  • 相关阅读:
    Mysql事务的隔离级别,ACID以及三要素的取舍(强一致性弱一致性)、ABA问题
    符合python风格的对象
    python重载运算符之左add右add(radd)
    pycham快捷键
    设置linux时间同步
    描述克隆linux的步骤,以及后续配置
    linux命令
    ransientException: find no Route:SELECT * FROM `ego`.`tb_content_category` LIMIT 0,1000‘
    linux修改vim /etc/profile 配置文件 修改不了
    web.xml
  • 原文地址:https://www.cnblogs.com/skyfei/p/1914826.html
Copyright © 2011-2022 走看看