无 BOM 的 UTF-8
我有一些 javascript 文件,我需要将它们保存为 UTF-8(无 BOM),每次我在 Notepad++ 中将它们转换为正确的格式时,它们都会恢复为带有 BOM 的 UTF-8我在 Visual Studio 中打开它们。我怎样才能阻止VS2010这样做呢?
另外一个问题,Visual Studio中无签名的UTF-8和无BOM的UTF-8一样吗?
I have javascript files that I need them to be saved in UTF-8 (without BOM), every time I convert them to the correct format in Notepad++, they are reverted back to UTF-8 with BOM when I open them in Visual Studio. How can I stop VS2010 from doing that?
Another question, is UTF-8 without signature in Visual Studio the same as UTF-8 without BOM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
BOM 或字节顺序标记有时很烦人。 Visual Studio 不会更改文件,除非您保存它(正如 Hans 所说)。
这是您问题的解决方案:
如果要使用其他编码保存文件,请选择另存为并扩展文件对话框中的保存按钮,然后选择“使用编码保存”。或者,如果您想永久删除此设置,只需打开“文件”菜单并选择“高级保存选项”,然后您应该选择“UTF-8 无签名”(这也回答了您的最后一个问题:)。是的“UTF-8无签名”与无BOM相同。
BOM or Byte Order Mark is sometimes quite annoying. Visual Studio does not change the file unless you save it (as Hans said).
And here is the solution to your problem:
If you want to save a file with other encodings, select save as and extend the save button in file dialog and select "Save with encoding". Or if you want to get rid of this setting permanently, just open File menu and select "Advanced save options" and there you should select "UTF-8 without signature" (and that also answered your last question :). Yes "UTF-8 without signature" is same as without BOM.
现在有图片。
转到
文件
->另存为
。然后在“保存”按钮上单击三角形,然后单击
使用编码保存...
。单击“确定”覆盖文件,然后从编码列表中找到
UTF-8 Without Signature
->单击确定
。希望这可以节省您一些时间。
Now with pictures.
Go to
File
->Save As
.Then on Save button click on triangle and click
Save with Encoding...
.Click ok to overwrite the file then from list of encodings find
UTF-8 Without signature
-> ClickOK
.Hope this saves you some time.
我创建了修复文件编码扩展,该扩展可防止 Visual Studio 2010+ 将 BOM 添加到 UTF -8 个文件。
I've created the Fix File Encoding extension that prevents Visual Studio 2010+ from adding BOM to UTF-8 files.
UTF-8 - “另存为”(无签名)默认 - 请求包含 UTF 的默认值 无签名
UTF-8 - "Save As" (Without Signature) Default - Request to include Default for UTF Without Signature
不幸的是,这不适用于 csproj 文件。没有“高级保存选项”,即使您已将 cs 文件设置为“UTF-8 无签名”,csproj 文件仍会使用 BOM 保存。如果您使用 VSS,它仍然会抱怨项目文件。
Unfortunately this does not work with csproj files. There is no "Advanced save option" and even though you have set it to "UTF-8 without signature" for a cs file, csproj files still are saved with BOM. If you use VSS it still complains about project files.
对于 Visual Studio Code 执行以下操作:
For Visual Studio Code do the following:
最近我发现了这个微型命令行工具,可以在任意 UTF-8 编码文件上添加或删除 BOM: UTF BOM 实用程序(新链接 at github)
有点缺点,你只能下载纯 C++ 源代码。您必须创建 makefile(例如使用 CMake)并自行编译,不提供二进制文件在此页面上。然而,对于软件开发人员来说这不应该是任何问题。
Recently I found this tiny command-line tool which adds or removes the BOM on arbitary UTF-8 encoded files: UTF BOM Utils (new link at github)
Little drawback, you can download only the plain C++ source code. You have to create the makefile (with CMake, for example) and compile it by yourself, binaries are not provided on this page. However, for a software developer this should not be any issue.
对于vs2010 c++,当源文件包含多字节字符(例如中文)时,UTF8 without BOM 会出现问题。
如果没有 BOM,这些字符将无法被正确识别,从而导致编译失败。
For vs2010 c++, there will be problems with UTF8 without BOM, when source files contain multi-byte characters(eg. Chinese).
Those characters will not be recognized correctly without BOM, and result in failed compling.
要对 .csproj 文件执行高级保存,必须首先通过右键单击项目并选择“卸载项目”来卸载项目。这将允许您使用不同的编码保存 .csproj 文件。
To perform Advanced Saving on a .csproj file, you must first unload the project by right clicking the project and selecting Unload Project. This will allow you to save the .csproj file with a different encoding.
即使使用 Dave81 的解决方案,每次我保存该 html 文件时,Visual Studio 2015 Community 仍然将我的文件恢复为 UTF8-BOM。
当我创建该 html 文件时,我右键单击该项目并选择“添加”,然后添加一个 HTML 文件。
默认情况下,Visual Studio 将在 HTML 文件中包含
标记。
只需删除标签,然后应用 Dave81 的解决方案,这次问题就真正消失了。
Visual Studio 似乎会解析您的 html 文件,当它看到该标记时,它会将文件转换为 UTF8-BOM,而不考虑原始文件格式(没有 BOM 的 UTF-8)。
我本来可以直接在 Dave81 的解决方案下发表评论,但我没有足够的积分来这样做......
Even with Dave81's solution, Visual Studio 2015 Community was still reverting my file to UTF8-BOM every single time I save that html file.
When I created that html file, I right-clicked on the project and selected "Add" then added an HTML file.
By default, Visual studio will include a
<meta charset="utf-8" />
tag in your HTML file.Simply removing the tag then applying Dave81's solution made the problem go away for real this time.
It seems the Visual Studio parse your html file and when it sees that tag it converts the file to UTF8-BOM without any consideration to the original file format (UTF-8 without BOM).
I would have made a comment directly under Dave81's solution, but I didn't have enough points to do so...
建议以无BOM为标准,
但是,当使用多个字节(例如[韩语或中文])时,如果 Visual Studio 中没有 BOM,则会显示警告消息。
实际上,我创建了一个韩语字符串数组,对应的字符串数组在运行时被破坏了。
使用Visual Studio时,根据MS警告消息,建议使用包含BOM的UTF -8。 (使用多字节字符时)
It is recommended that there is no BOM as standard,
but, when using multiple bytes such as [Korean or Chinese], a warning message is displayed when there is no BOM in Visual Studio.
Actually, I created a Korean String array, and the corresponding String array was broken at runtime.
When using Visual Studio, it is recommended to use UTF -8 including BOM according to MS warning message. (when using multi-byte characters)
工具->选项->文本编辑器中有选项
并选中选项自动检测没有签名的 utf8
there is option in tools->options->text editor
and check option auto detect utf8 withou signature
此代码将创建没有 BOM 的 UTF-8 文件
This code will create file in UTF-8 without BOM