1

When I generate a csv file from a string. I use the following code to encode a string to UTF-8:

byte[] bytes = Encoding.Default.GetBytes("√");
boolText = Encoding.UTF8.GetString(bytes);

But when I open a file is generated (by notepad++). It has "Encode in UTF-8 without BOM".

I need the file will be generated has UTF-8 so the symbol "√" will be show in excel.

Dzun Ho
  • 347
  • 5
  • 14
  • 1
    No, encoding it as UTF-8 would take a string and result in bytes. You're going the opposite direction, but in a weird way which is very likely to lose data. If you want a string with that character in, just use `string text = "√";`. It's very unclear what you're doing with the data afterwards... or where the BOM comes in, to be honest. – Jon Skeet Jan 18 '16 at 08:31
  • Have you tried `File.WriteAllText` with 3rd parameter encoding=utf8? – i486 Jan 18 '16 at 08:32
  • http://stackoverflow.com/questions/11701341/encoding-utf8-getstring-doesnt-take-into-account-the-preamble-bom – daryal Jan 18 '16 at 08:37
  • Look at the linked question. You need to create your own `UTF8Encoding` object to be able to specify if BOM should be added or not. – jgauffin Jan 18 '16 at 08:43

0 Answers0