[英]How to output Byte Order Mark when writing to TextWriter?
i am writing text to a TextWriter
.我正在向
TextWriter
写入文本。 i want the UTF-16 Byte Order Mark ( BOM
) to appear in the output:我希望 UTF-16 字节顺序标记 (
BOM
) 出现在输出中:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentEncoding = new UnicodeEncoding(true, true);
WriteStuffToTextWriter(context.Response.Output);
}
Except the output doesn't contain a byte order mark:除了输出不包含字节顺序标记:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 06 Sep 2012 21:09:23 GMT
X-AspNet-Version: 4.0.30319
Content-Disposition: attachment; filename="Transactions_Calendar_20120906.csv"
Cache-Control: private
Content-Type: text/csv; filename="Transactions_Calendar_20120906.csv"; charset=utf-16BE
Content-Length: 95022
Connection: Close
JobName,ShiftName,6////09////2012 12::::00::::00 АΜ,...
How do i tell a TextWriter
to write the encoding marker?我如何告诉
TextWriter
编写编码标记?
Note : The 2nd paramter in UnicodeEncoding
:注意:
UnicodeEncoding
的第二个参数:
context.Response.ContentEncoding = new UnicodeEncoding(true, true);
byteOrderMark
字节顺序标记
Type:System.Boolean
类型:
System.Boolean
true to specify that a Unicode byte order mark is provided;true指定提供 Unicode 字节顺序标记; otherwise, false .
否则为false 。
String zwnbsp = "\xfeff"; //Zero-width non-breaking space
//The Zero-width non-breaking space character ***is*** the Byte-Order-Mark (BOM).
String s = zwnbsp+"The quick brown fox jumped over the lazy dog.";
writer.Write(s);
At some point i realized how simple the solution is.在某些时候,我意识到解决方案是多么简单。
i used to think that the Unicode Byte-Order-Mark was some special signature.我曾经认为 Unicode 字节顺序标记是一些特殊的签名。 i used to think i had to carefully decide which byte sequence i wanted to output, in order to output the correct BOM:
我曾经认为我必须仔细决定我想输出哪个字节序列,才能输出正确的 BOM:
But since then i realized that byte Byte-Order-Mark is not some special byte sequence that you have to prepend to your file.但从那以后我意识到字节 Byte-Order-Mark不是您必须添加到文件中的特殊字节序列。
The BOM is just a Unicode character . BOM 只是一个Unicode 字符。 You don't output any bytes;
你不输出任何字节; you only output character
U+FEFF
.你只输出字符
U+FEFF
。 The very act of writing that character, the serializer will convert it to whatever encoding you're using for you.写一个字的非常行为,序列化将其转换为任何你正在使用你的编码。
The character U+feff
( ZERO WIDTH NO-BREAK SPACE
) was chosen for good reason.选择字符
U+feff
( ZERO WIDTH NO-BREAK SPACE
U+feff
ZERO WIDTH NO-BREAK SPACE
)是有充分理由的。 It's a space , so it has no meaning, and it is zero width , so you shouldn't even see it.它是一个空格,所以它没有意义,而且它的宽度为零,所以你甚至不应该看到它。
That means that my question is fundamentally flawed.这意味着我的问题从根本上是有缺陷的。 There is no such thing as "writing a byte-order-mark" .
没有“编写字节顺序标记”这样的东西。 You just make sure the first character you write out is
U+FEFF
.您只需确保您写出的第一个字符是
U+FEFF
。 In my case i am writing to a TextWriter
:就我而言,我正在给
TextWriter
写信:
void WriteStuffToTextWriter(TextWriter writer)
{
String csvExport = GetExportAsCSV();
writer.Write("\xfeff"); //Output unicode charcter U+FEFF as a byte order marker
writer.Write(csvExport);
}
The TextWriter
will handle converting the unicode character U+feff
into whatever byte encoding it has been configured to use. TextWriter
将处理将 unicode 字符U+feff
转换为它已配置使用的任何字节编码。
Note : Any code is released into the public domain.
注意:任何代码都发布到公共领域。 No attribution required.
不需要归属。
Write out context.Response.ContentEncoding.GetPreamble().写出 context.Response.ContentEncoding.GetPreamble()。 Take a look at Write text files without Byte Order Mark (BOM)?
看看写没有字节顺序标记(BOM)的文本文件?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.