我使用 Visual Studio 2010、C# 来阅读 Gmail 收件箱IMAP
,这很有吸引力,但我认为 Unicode 不完全受支持,因为我无法轻松获取波斯语(波斯语)字符串。
例如,我有我的字符串:سلام
,但IMAP
给了我:"=?utf-8?B?2LPZhNin2YU=?="
。
如何将其转换为原始字符串?将 utf-8 转换为字符串的任何提示?
我们来看看MIME编码的含义:
=?utf-8?B?...something...?=
^ ^
| +--- The bytes are Base64 encoded
|
+---- The string is UTF-8 encoded
因此,要对此进行解码,请...something...
取出您的字符串(2LPZhNin2YU=
在您的情况下),然后
反转Base64编码
var bytes = Convert.FromBase64String("2LPZhNin2YU=");
将字节解释为 UTF8 字符串
var text = Encoding.UTF8.GetString(bytes);
text
现在应该包含所需的结果。
这种格式的描述可以在 Wikipedia 中找到:
他在这里
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);
}));
}
以下方法将“=?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());
}