Convert question ANSI to UTF8 C #

I am having trouble converting a text file from ANSI to UTF8 in c#. I try to display the result in the browser.

So I have A text file with many accented characters. It is encoded in ANSI, so I have to convert it to utf8, because the accent mark of “?” appears in the browser instead of. No matter how I try to convert to UTF8, it is still “?”. However, if I convert the text file in Notepad to utf8, the accented characters will be fine.

This is the peace of the encoding code I made:

< p>

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);< br />
// reconstitute a string from the UTF-8 byte array
return UTF8Encoding.UTF8.GetString(myUTF8Bytes);
}

You know why this happens NS?

Do you have any idea why is this happening?

Yes, you are too late. You need to specify ANSI when reading a string from a file. In memory, it is always Unicode (UTF16).

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

So I have a text with many accented characters The file. It is encoded in ANSI, so I have to convert it to utf8, because the accent mark of “?” appears in the browser instead of. No matter how I try to convert to UTF8, it is still “?”. But if I Convert the text file in Notepad to utf8, then the accented characters will be fine.

This is the peace of the encoding code I made:

public string Encode(string text)
{
// encode the string as an ASCII byte array
byte[] myASCIIBytes = ASCIIEncoding.ASCII.GetBytes(text);
< br /> // 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 know why this happens?

Do you have any idea why is this happening?

Yes, you are too late It is necessary to specify ANSI when reading a string from a file. In memory, it is always Unicode (UTF16).

Leave a Comment

Your email address will not be published.