Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode non ascii characters in C# .NET

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?

like image 624
Reza S Avatar asked Nov 23 '11 19:11

Reza S


People also ask

What is non-ASCII character C?

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.

Does C use UTF 8?

Most C string library routines still work with UTF-8, since they only scan for terminating NUL characters.

What is a non-ascii characters example?

An example of a non-ASCII character is the Ñ. The URL can't contain any non-ASCII character or even a space.

What is ascii code in C?

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.


1 Answers

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.

like image 160
David Heffernan Avatar answered Sep 20 '22 01:09

David Heffernan