Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Decode "=?utf-8?B?...?=" to string in C#

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.

For instance I have my string: سلام, but IMAP gives me: "=?utf-8?B?2LPZhNin2YU=?=".

How can I convert it to original string? any tips from converting utf-8 to string?

like image 886
Ali_dotNet Avatar asked May 31 '12 06:05

Ali_dotNet


People also ask

How do you decode an encoded string?

So first, write the very first character of the encoded string and remove it from the encoded string then start adding the first character of the encoded string first to the left and then to the right of the decoded string and do this task repeatedly till the encoded string becomes empty.

Is UTF-8 a string?

UTF-8 vs.UTF-8 encodes a character into a binary string of one, two, three, or four bytes. UTF-16 encodes a Unicode character into a string of either two or four bytes. This distinction is evident from their names.

How do I encode a string in UTF-8?

In order to convert a String into UTF-8, we use the getBytes() method in Java. The getBytes() method encodes a String into a sequence of bytes and returns a byte array. where charsetName is the specific charset by which the String is encoded into an array of bytes.


2 Answers

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

So, to decode this, take the ...something... out of your string (2LPZhNin2YU= in your case) and then

  1. reverse the Base64 encoding

    var bytes = Convert.FromBase64String("2LPZhNin2YU=");
    
  2. interpret the bytes as a UTF8 string

    var text = Encoding.UTF8.GetString(bytes);
    

text should now contain the desired result.


A description of this format can be found in Wikipedia:

  • http://en.wikipedia.org/wiki/MIME#Encoded-Word
like image 77
Heinzi Avatar answered Sep 21 '22 09:09

Heinzi


What you have is a MIME encoded string. .NET does not include libraries for MIME decoding, but you can either implement this yourself or use a library.

like image 40
Jirka Hanika Avatar answered Sep 21 '22 09:09

Jirka Hanika