问题
I am developing an application in which i want to encode the Spanish text. But the problem is that,it doesn't encode the special characters such as á, é, í, ó, ú, ü,Á, É, Í, Ó, Ú, Ü,Ñ,ñ . How can i do this?i want to encode-decode the spanish text.
回答1:
For international support using simple UTF-8
encoding to encode/decode
your data should be enough.
Utf-8
has a beautiful capability to be able to read ASCII
with one byte, as ordinary ASCII, and Unicode characters with 2 bytes. So it's able "to shrink" when it's necesary.
For complete C#
documentation look on
UTF-8
EDIT
Encoding enc = new UTF8Encoding(true, true);
string value = " á, é, í, ó, ú, ü,Á, É, Í, Ó, Ú, Ü,Ñ,ñ ";
byte[] bytes= enc.GetBytes(value); //convert to BYTE array
//save in some file
//after can read from the file like
string decodedString = enc.GetString(byteArrayReadFromFile);
回答2:
ok,I am answering my own question ,Hope it will help someone; to print spanish or any other non-ascii character in the given string replace all non-ascii characters by their unicode escape character set E.g repalce á by \u00e1 And then simply print the string.
i.e
string str="árgrgrgrááhhttá";
str=str.Replace("á", "\u00e1");
来源:https://stackoverflow.com/questions/12576762/how-to-encode-decode-non-ascii-characters