0

When I load as bytes, and then convert it to utf8, i get a length of 438, if i just load it as utf8 directly, or just write textOut.lengt, i get 437 characters. The string looks the same for the eyes in debug, but the first character it say is different. Does anyone know the reason for this

var textOut = JsonConvert.SerializeObject(th);

File.WriteAllText(@"C:\Users\rooh\Music\jag_heter_roland.edit2", textOut, Encoding.UTF8);

var bytes = File.ReadAllBytes(@"C:\Users\rooh\Music\jag_heter_roland.edit2");
var str = System.Text.Encoding.UTF8.GetString(bytes);
Console.WriteLine(textOut.Length);
Console.WriteLine(str.Length);

1 Answers1

2

Presumably the first 3 bytes are 0xEFBBBF - the UTF-8 BOM.

When using ReadAllText, I suspect it will detect this and remove it; if you're doing the decode manually (GetString) it will preserve it as codepoint U+FEFF as the first character.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900