应以UTF-8格式保存源代码 [英] Should source code be saved in UTF-8 format

查看:140
本文介绍了应以UTF-8格式保存源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以UTF-8格式保存源代码有多重要?

How important is it to save your source code in UTF-8 format?

默认情况下,Windows上的Eclipse使用CP1252字符编码。 CP1251格式意味着可以保存非UTF-8字符,如果您从Word文档复制并粘贴注释,我已经看到这种情况。

Eclipse on Windows uses CP1252 character encoding by default. The CP1251 format means non UTF-8 characters can be saved and I have seen this happen if you copy and paste from a Word document for a comment.

我问的原因是因为习惯我设置的Maven编码是UTF-8格式,最近它已经捕获了一些不可映射的错误。

The reason I ask is because out of habit I set-up Maven encoding to be in UTF-8 format and recently it has caught a few non mappable errors.

(更新)请添加任何这样做的原因,为什么会有一些应该知道的常见问题?

(update) Please add any reasons for doing so and why, are there some common gotchas that should be known?

(更新)你的目标是什么?为了找到最好的做法,所以当问为什么我们应该使用UTF-8我有一个很好的答案,现在我不。

(update) What is your goal? To find the best practice so when ask why should we use UTF-8 I have a good answer, right now I don't.

推荐答案

你的目标是什么?平衡您的需求与这种选择的利弊。

What is your goal? Balance your needs against the pros and cons of this choice.

UTF-8优点


  • 允许使用所有字符文字,而不需要 \uHHHH 转义

  • allows use of all character literals without \uHHHH escaping

UTF-8缺点


  • 使用非ASCII字符文字,不含 \HHHH 增加角色腐败的风险


    • 字体和键盘问题可能会出现

    • 需要在所有工具(编辑器,编译器构建脚本,差异工具)中记录和强制使用UTF-8。

    • using non-ASCII character literals without \uHHHH increases risk of character corruption
      • font and keyboard issues can arise
      • need to document and enforce use of UTF-8 in all tools (editors, compilers build scripts, diff tools)

      ASCII优点


      • 字符/字节映射由广泛的编码共享


        • 使源文件非常便于携带

        • 通常不需要指定编码元数据(因为如果文件被重新编码为UTF-8,则文件将是相同的s-1252,ISO 8859-1以及UTF-16和/或EBCDIC的大部分内容)

        ASCII缺点


        • 限定字符集

        • 这不是20世纪60年代

        注意:ASCII是7位,而不是扩展,不要混淆与Windows-1252,ISO 8859-1或其他任何东西。

        这篇关于应以UTF-8格式保存源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆