如何在C#中将“=?utf-8?B?...?=”解码为字符串

时间:2012-05-31 06:57:50

标签: c# string unicode utf-8

我使用Visual Studio 2010,C#使用IMAP阅读Gmail收件箱,它可以作为魅力,但我认为Unicode不完全支持,因为我无法轻松获得波斯语(波斯语)字符串。

例如我有我的字符串:سلام,但IMAP给了我:"=?utf-8?B?2LPZhNin2YU=?="

如何将其转换为原始字符串?将utf-8转换为字符串的任何提示?

4 个答案:

答案 0 :(得分:20)

让我们看一下MIME编码的含义:

=?utf-8?B?...something...?=
    ^   ^
    |   +--- The bytes are Base64 encoded
    |
    +---- The string is UTF-8 encoded

因此,要对此进行解码,请从字符串中取出...something...(在您的情况下为2LPZhNin2YU=),然后

  1. 反转Base64编码

    var bytes = Convert.FromBase64String("2LPZhNin2YU=");
    
  2. 将字节解释为UTF8字符串

    var text = Encoding.UTF8.GetString(bytes);
    
  3. text现在应该包含所需的结果。


    此格式的说明可在维基百科中找到:

答案 1 :(得分:3)

您拥有的是MIME编码字符串。 .NET不包含用于MIME解码的库,但您可以实现此yourself或使用library

答案 2 :(得分:0)

他在这里

    public static string Decode(string s)
    {
        return String.Join("", Regex.Matches(s ?? "", @"(?:=\?)([^\?]+)(?:\?B\?)([^\?]*)(?:\?=)").Cast<Match>().Select(m =>
        {
            string charset = m.Groups[1].Value;
            string data = m.Groups[2].Value;
            byte[] b = Convert.FromBase64String(data);
            return Encoding.GetEncoding(charset).GetString(b);
        }));
    }

答案 3 :(得分:0)

以下方法将诸如“=?utf-8?B?...”或“=?utf-8?Q?...”之类的字符串解码为普通字符串。编码(如“utf-8”)是自动选择的。不使用正则表达式。 C#

public static string DecodeQuotedPrintables(string InputText)
    {
        var ResultChars = new List<char>();
        Encoding encoding;
        for (int i= 0; i < InputText.Length; i++)
        {
            var CurrentChar = InputText[i];
            switch (CurrentChar)
            {
                case '=':
                    if((i + 1) < InputText.Length && InputText[i+1] == '?')
                    {
                        // Encoding
                        i += 2;
                        int StIndex = InputText.IndexOf('?', i);
                        int SubStringLength = StIndex - i;
                        string encodingName = InputText.Substring(i, SubStringLength);
                        encoding = Encoding.GetEncoding(encodingName);
                        i += SubStringLength + 1;

                        //Subencoding
                        StIndex = InputText.IndexOf('?', i);
                        SubStringLength = StIndex - i;
                        string SubEncoding = InputText.Substring(i, SubStringLength);
                        i += SubStringLength + 1;

                        //Text message
                        StIndex = InputText.IndexOf("?=", i);
                        SubStringLength = StIndex - i;
                        string Message = InputText.Substring(i, SubStringLength);
                        i += SubStringLength + 1;

                        // encoding
                        switch (SubEncoding)
                        {
                            case "B":
                                var base64EncodedBytes = Convert.FromBase64String(Message);
                                ResultChars.AddRange(encoding.GetString(base64EncodedBytes).ToCharArray());

                                // skip space #1
                                if ((i + 1) < InputText.Length && InputText[i + 1] == ' ')
                                {
                                    i++;
                                }
                                break;

                            case "Q":
                                var CharByteList = new List<byte>();
                                for (int j = 0; j < Message.Length; j++)
                                {
                                    var QChar = Message[j];
                                    switch (QChar)
                                    {
                                        case '=':
                                            j++;
                                            string HexString = Message.Substring(j, 2);
                                            byte CharByte = Convert.ToByte(HexString, 16);
                                            CharByteList.Add(CharByte);
                                            j += 1;
                                            break;

                                        default:
                                            // Decode charbytes #1
                                            if (CharByteList.Count > 0)
                                            {   
                                                var CharString = encoding.GetString(CharByteList.ToArray());
                                                ResultChars.AddRange(CharString.ToCharArray());
                                                CharByteList.Clear();
                                            }

                                            ResultChars.Add(QChar);
                                            break;
                                    }
                                }

                                // Decode charbytes #2
                                if (CharByteList.Count > 0)
                                {
                                    var CharString = encoding.GetString(CharByteList.ToArray());
                                    ResultChars.AddRange(CharString.ToCharArray());
                                    CharByteList.Clear();
                                }
                                
                                // skip space #2
                                if ((i + 1) < InputText.Length && InputText[i + 1] == ' ')
                                {
                                    i++;
                                }
                                break;

                            default:
                                throw new NotSupportedException($"Decode quoted printables: unsupported subencodeing: '{SubEncoding}'");
                        }
                    }
                    else
                        ResultChars.Add(CurrentChar);
                    break;

                default:
                    ResultChars.Add(CurrentChar);
                    break;
            }
        }

        return new string(ResultChars.ToArray());
    }