从UTF8转换为ASCII [英] Conversion from UTF8 to ASCII

查看:195
本文介绍了从UTF8转换为ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从UTF8编码存储的XML文件读取的文本。 C#读完它,我检查了调试器,但是当我尝试将其转换为ASCII,以保存在另一个文件中,我得到一个?在有矛盾角色的地方,例如,这个文本:

I have a text read from a XML file stored in UTF8 encoding. C# reads it perfectly, I checked with the debugger, but when I try to convert it to ASCII to save it in another file I get a ? char in places where there was a conflicting character. For instance, this text:

string s = "La introducción masiva de las nuevas tecnologías de la información";

将另存为

"La introducci?n masiva de las nuevas tecnolog?as de la informaci?n"


$ b $我不能用拉丁语(a,e,i,o,u)替换它们,因为西班牙语中的一些单词会错过这个意思。我已经尝试过这个和<一个href =https://stackoverflow.com/questions/497782/how-to-convert-a-string-from-utf8-to-ascii-single-byte-in-c>这个的问题没有成功。所以我希望有人可以帮助我。第二个选择的答案甚至没有编译...!

I cannot just replace them for their latin (a, e, i, o, u) vowels because some words in spanish would miss the sense. I've already tried this and this questions with no sucess. So Im hoping someone can help me. The selected answer in the second one didnt even compiled...!

如果有人想看看,我的代码是这样的:

In case someone wants to take a look, my code is this one:

private void WriteInput( string input )
{
   byte[] byteArray = Encoding.UTF8.GetBytes(input);
   byte[] asciiArray = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, byteArray);
   string finalString = Encoding.ASCII.GetString(asciiArray);

   string inputFile = _idFile + ".in";
   var batchWriter = new StreamWriter(inputFile, false, Encoding.ASCII);
   batchWriter.Write(finalString);
   batchWriter.Close();
}


推荐答案

ASCII。查看ASCII表,例如维基百科,以验证这一点。您可能对Windows 1252编码或扩展ASCII感兴趣,因为它有时被称为,其中包含许多重音字符的代码点,包括西班牙语。

Those characters have no mapping in ASCII. Review an ASCII table, like Wikipedia's, to verify this. You might be interested in the Windows 1252 encoding, or "extended ASCII", as it's sometimes called, which has code points for many accented characters, Spanish included.

var input = "La introducción masiva de las nuevas tecnologías de la información";
var utf8bytes = Encoding.UTF8.GetBytes(input);
var win1252Bytes = Encoding.Convert(
                Encoding.UTF8, Encoding.GetEncoding("windows-1252"), utf8bytes);
File.WriteAllBytes(@"foo.txt", win1252Bytes);

这篇关于从UTF8转换为ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆