Search code examples
c#stringencoderepresentation

How to convert string to UTF-8 string representation?


As i mentioned in the title. For example:

If the input looks like:

FW: Błąd zakładania FA/PA

I would like to receive:

=?utf-8?B?Rlc6IELFgsSFZCB6YWvFgmFkYW5pYSBGQS9QQQ==?=

Thanks for help.


Solution

  • In this specific case Base64 encoded text exists in this MIME encoded string after the B?upto the next ?

    string mimed = "=?utf-8?B?Rlc6IELFgsSFZCB6YWvFgmFkYW5pYSBGQS9QQQ==?=";
    
    mimed = mimed.Substring(10, mimed.IndexOf("?", 10) - 10);
    
    string result = Encoding.UTF8.GetString(Convert.FromBase64String(mimed));
    

    The reverse:

    result = string.Format("=?utf-8?B?{0}?=", Convert.ToBase64String(Encoding.UTF8.GetBytes(@"FW: Błąd zakładania FA/PA")));