简体   繁体   中英

Extended ASCII characters such as euro symbol being converted to its unicode equivalent

I have the euro symbol stored in an MS-Access database table:

SELECT
CurrencySymbol,
Len(CurrencySymbol) AS DataLength,
Asc(CurrencySymbol) AS AsciiCode
FROM table1;

CurrencySymbol DataLength AsciiCode
-------------- ---------- ---------
€              1          128

And here is the .NET code I am using to read this table:

OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + args[0]);
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT * FROM [table1]", connection);
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    for (i = 0, j = reader.FieldCount; i < j; i++)
    {
        System.Diagnostics.Debug.Print(reader.GetValue(i));
    }
}

Originally, I was writing the data to a text file using StreamWriter . I noticed that the euro symbol was written as € which probably is the unicode euro symbol encoded in UTF-8. Debugger results:

reader.GetValue(i).ToString()                  -> "€"
reader.GetValue(i).ToString().ToCharArray()[0] -> 8364 '€'

How can I enforce .NET to spit out output the extended ASCII characters as-is? The characters are supposed to be written in a CSV file.

The fact that these two lines:

reader.GetValue(i).ToString()                  -> "€"
reader.GetValue(i).ToString().ToCharArray()[0] -> 8364 '€'

do what you want tells me we can stop looking at data-access and MS Access, 'cos that is all working fine. The problem is simply: writing that to a file. The trick, then, is to be explicit when you create the StreamWriter . If you look at the StreamWriter constructors, you'll see that some take an Encoding . If you leave it blank, it will default to UTF-8 . So: don't leave it blank. Explicitly pass in your chosen Encoding . I would recommend you figure out exactly which code-page you mean, and use:

const int CodePage = ....; // TODO: only you know this
var enc = Encoding.GetEncoding(CodePage);
using(var file = File.Create(path))
using(var writer = new StreamWriter(file, enc)) {
   ... // write the contents
}

You could also use Encoding.Default (the system's default ANSI code-page), but that is a bit hit and miss.

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