简体   繁体   中英

ansi to unicode conversion

While parsing certain documents, I get the character code 146, which is actually an ANSI number. While writing the char to text file, nothing is shown. If we write the char as Unicode number- 8217, the character is displayed fine.

Can anyone give me advice on how to convert the ANSI number 146 to Unicode 8217 in C#.

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

Thanks

"ANSI" is really a misnomer - there are many encodings often known as "ANSI". However, if you're sure you need code page 1252, you can use:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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