Skip to main content
 首页 » 编程设计

c#之如何编码和解码非 Ascii 字符

2024年02月20日14zhwl

我正在开发一个应用程序,我想在其中对西类牙语文本进行编码。 但问题是,它不编码特殊字符,如 á, é, í, ó, ú, ü,Á, É, Í, Ó, Ú, Ü,Ñ,ñ . 我该怎么做?我想对西类牙语文本进行编码解码。

请您参考如下方法:

对于使用简单的 UTF-8 编码来编码/解码 的国际支持,您的数据应该足够了。

Utf-8 有一个很好的能力,可以读取一个字节的 ASCII,如普通的 ASCII 和 2 个字节的 Unicode 字符。因此它可以在需要时“收缩”。

完整的 C# 文档请看 UTF-8

编辑

  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);