Text Decoding Problem
        Posted  
        
            by Jason Miesionczek
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Jason Miesionczek
        
        
        
        Published on 2010-04-05T15:13:56Z
        Indexed on 
            2010/04/05
            15:23 UTC
        
        
        Read the original article
        Hit count: 413
        
c#
|text-decoding
So given this input string:
=?ISO-8859-1?Q?TEST=2C_This_Is_A_Test_of_Some_Encoding=AE?=
And this function:
private string DecodeSubject(string input)
        {
            StringBuilder sb = new StringBuilder();
            MatchCollection matches = Regex.Matches(inputText.Text, @"=\?(?<encoding>[\S]+)\?.\?(?<data>[\S]+[=]*)\?=");
            foreach (Match m in matches)
            {
                string encoding = m.Groups["encoding"].Value;
                string data = m.Groups["data"].Value;
                Encoding enc = Encoding.GetEncoding(encoding.ToLower());
                if (enc == Encoding.UTF8)
                {
                    byte[] d = Convert.FromBase64String(data);
                    sb.Append(Encoding.ASCII.GetString(d));
                }
                else
                {                    
                    byte[] bytes = Encoding.Default.GetBytes(data);
                    string decoded = enc.GetString(bytes);
                    sb.Append(decoded);
                }
            }
            return sb.ToString();
        }
The result is the same as the data extracted from the input string. What am i doing wrong that this text is not getting decoded properly?
© Stack Overflow or respective owner