简体   繁体   English

ansi 到 unicode 转换

[英]ansi to unicode conversion

While parsing certain documents, I get the character code 146, which is actually an ANSI number.在解析某些文档时,我得到了字符代码 146,它实际上是一个 ANSI 数字。 While writing the char to text file, nothing is shown.将字符写入文本文件时,没有显示任何内容。 If we write the char as Unicode number- 8217, the character is displayed fine.如果我们将字符写为 Unicode 编号 - 8217,则字符显示正常。

Can anyone give me advice on how to convert the ANSI number 146 to Unicode 8217 in C#.谁能给我关于如何在 C# 中将 ANSI 编号 146 转换为 Unicode 8217 的建议。

reference: http://www.alanwood.net/demos/ansi.html参考: http://www.alanwood.net/demos/ansi.html

Thanks谢谢

"ANSI" is really a misnomer - there are many encodings often known as "ANSI". “ANSI”确实是用词不当——有许多编码通常被称为“ANSI”。 However, if you're sure you need code page 1252, you can use:但是,如果您确定需要代码页 1252,则可以使用:

Encoding encoding = Encoding.GetEncoding(1252);
using (TextReader reader = File.OpenText(filename, encoding))
{
    // Read text and use it
}

or或者

Encoding encoding = Encoding.GetEncoding(1252);
string text = File.ReadAllText(filename, encoding);

That's for reading a file - writing a file is the same idea.那是为了读取文件 - 写入文件是相同的想法。 Basically when you're converting from binary (eg file contents) to text, use an appropriate Encoding object.基本上,当您从二进制(例如文件内容)转换为文本时,请使用适当的Encoding object。

My recommendation would be to read Joel's "Absolute Minimum Every Software Developer Must Know About Unicode and Character Sets . There's quite a lot involved in your question and my experience has been that you'll just struggle against the simple answers if you don't understand these basics. It takes around 15 minutes to read.我的建议是阅读Joel 的“每个软件开发人员必须了解的关于 Unicode 和字符集的绝对最低要求。您的问题涉及很多内容,我的经验是,如果您不明白,您只会与简单的答案作斗争这些基础知识。阅读大约需要 15 分钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM