Conversion from UTF8 to ASCII

后端 未结 2 415
小鲜肉
小鲜肉 2020-12-14 10:59

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 f

相关标签:
2条回答
  • 2020-12-14 11:37

    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);
    
    0 讨论(0)
  • 2020-12-14 11:49

    Can't be done. ASCII does not have those letters, so the best you can do is to URL-encode or unicode-escape-encode them.

    0 讨论(0)
提交回复
热议问题