谁能解释在新实例化的 utf8 编码上调用 GetPreamble() 与在 Encoding 类中可用的公共(public)编码之间的区别?
byte[] p1 = Encoding.UTF8.GetPreamble();
byte[] p2 = new UTF8Encoding().GetPreamble();
p1 是正常的 3 字节 utf-8 前导码,但 p2 最终为空,这似乎很错误。
最佳答案
不同的是Enconding的UTF8属性是这样创建的
new UTF8Encoding(true)
这表明 encoderShouldEmitUTF8Identifier = true 所以 3 字节前导码在那里
以及您对默认构造函数的调用
new UTF8Encoding()
相当于
new UTF8Encoding(false)
要获得相同的结果:
byte[] p1 = Encoding.UTF8.GetPreamble();
byte[] p2 = new UTF8Encoding(true).GetPreamble();
关于c# - (new Utf8Encoding()).GetPreamble() 的前导为空 - 很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2027702/