How to encode & decode non Ascii characters?

别等时光非礼了梦想. 提交于 2021-02-10 18:44:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!