Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Encoding.UTF8.GetBytes() create a BOM?

I'm making an HTTP POST request with this:

byte[] postBuffer = Encoding.UTF8.GetBytes(postStr);

So far, this seems working fine but I'm not sure if this will always work, because Encoding.UTF8 means UTF8 WITH BOM. When I create local files with StreamWriter, always use the default encoding which is the same as new UTF8Encoding(false) in order to write WITHOUT BOM. So wonder if the same is true for calling GetBytes() method too.

In this case, isn't there any difference between above and below line?

byte[] postBuffer = new UTF8Encoding().GetBytes(postStr);

I tested several times myself but still not sure 100%, so asking here.

like image 640
Jenix Avatar asked Oct 26 '17 13:10

Jenix


1 Answers

The GetBytes method never returns a stream of bytes prefaced with a BOM. The BOM can be retrieve by using the GetPreAmble method, which returns nothing when the encoder is instantiated with false.

See the extensive Remarks section on Microsoft Documentation for more details.

like image 182
MarkO Avatar answered Oct 21 '22 20:10

MarkO