Decodes a QuotedPrintable encoded string

it2022-05-09  16

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

 

 

转载于:https://www.cnblogs.com/skyfei/archive/2010/12/23/1914826.html


最新回复(0)