Convert ANSI issue to UTF8 C #

I have a problem converting a text file from ANSI to UTF8 in C #. I am trying to display the results in a browser.

So, I have this text file with a lot of accent characters. It is encoded in ANSI, so I have to convert it to utf8 because "?" Appears in the browser instead of accents. No matter how I tried to convert to UTF8, it was still a “?”. But if I convert a text file to notepad ++ in utf8, then the accent characters will be removed.

here is the world of coding code 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 an idea why this is happening?

+5
source share
6 answers

Do you have an idea why this is happening?

, . ANSI . Unicode (UTF16).

+14

ASCII, ( ), ASCII 127 (7 ) .

. string in.net UTF-16, string, byte[] .

, : ( , ANSI Latin1)

public byte[] Encode(string text)
{
    return Encoding.GetEncoding(1252).GetBytes(text);
}

, , :

public string Decode(byte[] data)
{
    return Encoding.GetEncoding(1252).GetString(data);
}
+7

, , :

byte[] ansiBytes = File.ReadAllBytes("inputfilename.txt");
var utf8String = Encoding.Default.GetString(ansiBytes);
File.WriteAllText("outputfilename.txt", utf8String);
+4

, Notepad ++, Byte-Order-Mark, , UTF8 . , , , DTD, XML ..

0

, , , string text . , . , , , .

0

All Articles