Encode non ascii characters in C # .NET

I want to add a custom header to emails sent by my application. The header name can only contain ASCII characters, but for the value and users can potentially enter UTF-8 characters, and I have to base64 encode them. I also need to decode them back to UTF-8 to show them to the user in the user interface.

What is the best way to do this?

+4
source share
2 answers

To convert from a .net string to a base 64, using UTF8 as the base encoding:

string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(text)); 

And to cancel the process:

 string text = Encoding.UTF8.GetString(Convert.FromBase64String(base64)); 

You can absolutely skip step UTF8. However, UTF8 usually results in a lower payload, which is UTF16 and therefore I would recommend using UTF8 as the base encoding.


I'm not sure what you mean when you say that the user can enter UTF8 characters. The .net structure uses UTF16 as the encoding of the working string. The strings you use in .net are always encoded using UTF16. Perhaps you just mean that the text may contain characters other than ASCII.

+8
source

To encode a string:

 var someUtf8Str = "ఠఠfoobarఠఠ"; var bytes = Encoding.UTF8.GetBytes(someUtf8Str); var asBase64Str = Convert.ToBase64String(bytes); 

To decode it:

 var bytes = Convert.FromBase64String(asBase64Str); var asUtf8Str = Encoding.UTF8.GetString(bytes); 
+1
source

Source: https://habr.com/ru/post/1382760/


All Articles