A direct conversion from ANSI to ASCII might not always be possible, since ANSI is a superset of ASCII.
You can try converting to UTF-8 using Encoding
, though:
Encoding ANSI = Encoding.GetEncoding(1252);
byte[] ansiBytes = ANSI.GetBytes(str);
byte[] utf8Bytes = Encoding.Convert(ANSI, Encoding.UTF8, ansiBytes);
String utf8String = Encoding.UTF8.GetString(utf8bytes);
Of course you can replace UTF8 with ASCII, but that doesn't really make sense since:
- if the original string doesn't contain any byte > 126, then it's already ASCII
- if the original string does contain one or more bytes > 126, then those bytes will be lost
UPDATE:
In response to the updated question, you can use BinaryReader
like this:
BinaryReader reader = new BinaryReader(File.Open("foo.txt", FileMode.Open),
Encoding.GetEncoding(1252));