I want to add a custom header to the emails my application is sending out. The header name can only contain ASCII chars, but for the value and users could potentially enter UTF-8 characters and I have to base64-encode them. Also I have to decode them back to UTF-8 to show them back to the user in the UI.
What's the best way to do this?
Non-ASCII characters are those that are not encoded in ASCII, such as Unicode, EBCDIC, etc. ASCII is limited to 128 characters and was initially developed for the English language.
Most C string library routines still work with UTF-8, since they only scan for terminating NUL characters.
An example of a non-ASCII character is the Ñ. The URL can't contain any non-ASCII character or even a space.
In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself. This integer value is the ASCII code of the character. For example, the ASCII value of 'A' is 65.
To convert from a .net string to base 64, using UTF8 as the underlying encoding:
string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
And to reverse the process:
string text = Encoding.UTF8.GetString(Convert.FromBase64String(base64));
It is perfectly possible to skip the UTF8 step. However, UTF8 typically results in a smaller payload that UTF16 and so I would recommend using UTF8 as the underlying encoding.
I'm not sure what you mean when you say that the user can enter UTF8 characters. The .net framework uses UTF16 as its working string encoding. The strings you use in .net are always encoded with UTF16. Perhaps you are just meaning that the text can contain non-ASCII characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With