Converting problem ANSI to UTF8 C#

JahManCan picture JahManCan · Sep 23, 2010 · Viewed 44.9k times · Source

I have a problem with converting a text file from ANSI to UTF8 in c#. I try to display the results in a browser.

So I have a this text file with many accent character in it. Its encoded in ANSI, so I have to convert it to utf8 because in the browser instead of the accentchars appearing "?". No matter how I tried to convert to UTF8 it was still a "?". But if I convert the text file in notepad++ to utf8 then the accent chars are desplayed good.

here is a peace of encoding code that I made:

    public string Encode(string text)
    {
        // encode the string as an ASCII byte array
        byte[] myASCIIBytes = ASCIIEncoding.ASCII.GetBytes(text);

        // convert the ASCII byte array to a UTF-8 byte array
        byte[] myUTF8Bytes = ASCIIEncoding.Convert(ASCIIEncoding.ASCII, UTF8Encoding.UTF8, myASCIIBytes);

        // reconstitute a string from the UTF-8 byte array 
        return UTF8Encoding.UTF8.GetString(myUTF8Bytes);
    }

Do you have any idea why is this happening?

Answer

Henk Holterman picture Henk Holterman · Sep 23, 2010

Do you have any idea why is this happening?

Yes, you're too late. You need to specify ANSI when you read the string from file. In memory it's always Unicode (UTF16).