如何解码"=?utf-8?B?...?="串入C# [英] How to Decode "=?utf-8?B?...?=" to string in C#
问题描述
我使用Visual Studio 2010,C#通过IMAP
读取Gmail收件箱,它很吸引人,但我认为不完全支持Unicode,因为我无法轻松获得波斯语(波斯语)字符串.
I use Visual Studio 2010, C# to read Gmail inbox using IMAP
, it works as a charm, but I think Unicode is not fully supported as I cannot get Persian (Farsi) strings easily.
例如,我有我的字符串:سلام
,但是IMAP
给了我:"=?utf-8?B?2LPZhNin2YU=?="
.
For instance I have my string: سلام
, but IMAP
gives me: "=?utf-8?B?2LPZhNin2YU=?="
.
如何将其转换为原始字符串?从转换utf-8到字符串的任何提示吗?
How can I convert it to original string? any tips from converting utf-8 to string?
推荐答案
让我们看一下MIME编码的含义:
Let's have a look at the meaning of the MIME encoding:
=?utf-8?B?...something...?=
^ ^
| +--- The bytes are Base64 encoded
|
+---- The string is UTF-8 encoded
因此,要对此进行解码,请从字符串中删除...something...
(在您的情况下为2LPZhNin2YU=
),然后
So, to decode this, take the ...something...
out of your string (2LPZhNin2YU=
in your case) and then
-
反转Base64编码
reverse the Base64 encoding
var bytes = Convert.FromBase64String("2LPZhNin2YU=");
将字节解释为UTF8字符串
interpret the bytes as a UTF8 string
var text = Encoding.UTF8.GetString(bytes);
text
现在应包含所需的结果.
text
should now contain the desired result.
有关这种格式的说明,请参见Wikipedia:
A description of this format can be found in Wikipedia:
这篇关于如何解码"=?utf-8?B?...?="串入C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!