Converting problem ANSI to UTF8 C#

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).


When you convert to ASCII you immediately lose all non-English characters (including ones with accent) because ASCII has only 127 (7 bits) of characters.

You do strange manipulation. string in .net is in UTF-16, so once you return string, not byte[] this doesn't matter.

I think you should do: (I guess by ANSI you mean Latin1)

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

Since the question was not very clear there is a reasonable remark that you might actually need this one:

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

This is probably the easiest way:

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