我们知道类UTF8Encoding
的构造函数可以接收一个可选参数:bool
指定编码器是否应该提供字节顺序标记(BOM)。
但是,使用两种方法对相同文本进行编码时,输出相同:
string text = "Hello, world!";
byte[] withBom= new UTF8Encoding(true).GetBytes(text);
byte[] withoutBom = new UTF8Encoding(false).GetBytes(text);
withBom
和withoutBom
都有相同的内容,其中一个字节甚至比另一个字节多一个字节。
为什么会这样?为什么没有将字节顺序标记添加到withBom
?
答案 0 :(得分:4)
构造函数中的BOM参数不会影响GetBytes
的结果,它会影响GetPreamble
的结果。用户需要手动附加。
byte[] bom = new UTF8Encoding(true).GetPreamble(); // 3 bytes
byte[] noBom = new UTF8Encoding(false).GetPreamble(); // 0 bytes
答案 1 :(得分:2)
通过UTF8Encoding.GetPreamble
方法返回BOM:
UTF8Encoding enc = new UTF8Encoding(true);
byte[] withBom = enc.GetPreamble().Concat(enc.GetBytes(text)).ToArray();