I have a text file (UTF-8 encoding) with contents "test". I try to get the byte array from this file and convert to string, but it contains one strange character. I use the following code:
var path = @"C:\Users\Tester\Desktop\test\test.txt"; // UTF-8
var bytes = File.ReadAllBytes(path);
var contents1 = Encoding.UTF8.GetString(bytes);
var contents2 = File.ReadAllText(path);
Console.WriteLine(contents1); // result is "?test"
Console.WriteLine(contents2); // result is "test"
conents1
is different than contents2
- why?