Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't the Byte Order Mark emitted from UTF8Encoding.GetBytes?

The snippet says it all :-)

UTF8Encoding enc = new UTF8Encoding(true/*include Byte Order Mark*/);
byte[] data = enc.GetBytes("a");
// data has length 1.
// I expected the BOM to be included. What's up?
like image 552
xyz Avatar asked Jan 07 '09 16:01

xyz


2 Answers

You wouldn't want it to be used for every call to GetBytes, otherwise you'd have no way of (say) writing a file a line at a time.

By exposing it with GetPreamble, callers can insert the preamble just at the appropriate point (i.e. at the start of their data). I agree that the documentation could be a lot clearer though.

like image 137
Jon Skeet Avatar answered Sep 19 '22 18:09

Jon Skeet


Thank you both. The following works, and LINQ makes the combination simple :-)

UTF8Encoding enc = new UTF8Encoding(true);
byte[] data = enc.GetBytes("a");
byte[] combo = enc.GetPreamble().Concat(data).ToArray();
like image 44
xyz Avatar answered Sep 18 '22 18:09

xyz