Erik Volkening
Erik Volkening

Reputation: 67

Convert unicode characters with c#

I have a client who has asked us to write a c# application that takes data from their database and output it to a .csv file. So far so good.

This DB contains some unicode characters and when the client opens up the .csv with Excel those characters look "weird". Ex: x0096 looks like an A with a carrot on top next to the Euro currency sign, when the client thinks it should look like a Dash.

So I have been asked to make those characters look "not wierd".

I have written code for each weird character (I have like 12 of the below lines).

input = input.Replace((char)weirdCharacter, (char)normalCharacter);

There has got to be a better way.

Upvotes: 1

Views: 1579

Answers (2)

JLRishe
JLRishe

Reputation: 101748

As Vincent James says, if this is an encoding issue, then the ideal way to fix this is to just use the right encoding when you decode/encode the value, but if that still doesn't work...

I think this is pretty straightforward. What do you think?:

Dictionary<char, char> substitutions = new Dictionary<char, char> {
  {'\0x0096', 'F'}, {'\0x0101', 'O'}, {'\0x0121', 'O'}, ...
};

foreach(KeyValuePair<char, char> pair in substitutions)
{
   input.Replace(pair.Key, pair.Value);
}

Upvotes: 0

Vincent James
Vincent James

Reputation: 1158

I had the same problem when I was generating HTML files. The solution for me was to change the encoding of my output file.

StreamWriter swHTMLPage = 
                new System.IO.StreamWriter(OutputFileName, false, Encoding.UTF8);

Once I added the Encoding.UTF8 parameter the characters started displaying correctly. I don't know if this can be applied to your solution though since Excel is involved, but I am betting it can be.

Upvotes: 1

Related Questions