{"id":2670,"date":"2019-02-14T10:00:40","date_gmt":"2019-02-14T02:00:40","guid":{"rendered":"https:\/\/www.aoisnow.net\/blog\/?p=2670"},"modified":"2019-02-14T10:00:40","modified_gmt":"2019-02-14T02:00:40","slug":"tont-40093-%e6%9c%89%e4%ba%9b%e6%96%87%e4%bb%b6%e5%9c%a8%e8%ae%b0%e4%ba%8b%e6%9c%ac%e9%87%8c%e6%89%93%e5%bc%80%e6%97%b6%e6%80%aa%e6%80%aa%e7%9a%84","status":"publish","type":"post","link":"https:\/\/www.aoisnow.net\/blog\/archives\/2670","title":{"rendered":"TONT 40093 \u6709\u4e9b\u6587\u4ef6\u5728\u8bb0\u4e8b\u672c\u91cc\u6253\u5f00\u65f6\u602a\u602a\u7684"},"content":{"rendered":"<p>\u8054\u901a\u7535\u6c60\u3002<!--more--><\/p>\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20040324-00\/?p=40093\" target=\"_blank\" rel=\"noopener\">https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20040324-00\/?p=40093<\/a><\/p>\n<p><a href=\"http:\/\/weblogs.asp.net\/cumpsd\/archive\/2004\/02\/27\/81098.aspx\" target=\"_blank\" rel=\"noopener\">David Cumps discovered that certain text files come up strange in Notepad.<\/a><\/p>\n<p><a href=\"http:\/\/weblogs.asp.net\/cumpsd\/archive\/2004\/02\/27\/81098.aspx\" target=\"_blank\" rel=\"noopener\">David Cumps\u53d1\u73b0\u67d0\u4e9b\u7279\u5b9a\u7684\u6587\u672c\u6587\u4ef6\u5728\u8bb0\u4e8b\u672c\u4e2d\u6253\u5f00\u65f6\u597d\u50cf\u6709\u4e9b\u602a\u602a\u7684\u3002<\/a><\/p>\n<p>The reason is that Notepad has to edit files in a variety of encodings, and when its back against the wall, sometimes it&#8217;s forced to guess.<\/p>\n<p>\u539f\u56e0\u662f\u8bb0\u4e8b\u672c\u9700\u8981\u5e94\u5bf9\u4e0d\u540c\u7f16\u7801\u7684\u6587\u4ef6\uff0c\u800c\u5f53\u88ab\u903c\u5230\u6ca1\u6cd5\u7684\u65f6\u5019\uff0c\uff08\u6587\u4ef6\u7684\u7f16\u7801\uff09\u4e5f\u5c31\u53ea\u80fd\u9760\u731c\u4e86\u3002<\/p>\n<p>Here&#8217;s the file &#8220;Hello&#8221; in various encodings:<\/p>\n<p>\u4ee5\u4e0b\u662f\u5305\u542b\u5b57\u7b26\u4e32\u300eHello\u300f\u7684\u6587\u672c\u6587\u4ef6\uff0c\u4f46\u7f16\u7801\u4e0d\u540c\uff1a<\/p>\n<p>48 65 6C 6C 6F<\/p>\n<p>This is the traditional ANSI encoding.\uff08\u8fd9\u662f\u4f20\u7edf\u7684ANSI\u7f16\u7801\u3002\uff09<\/p>\n<p>48 00 65 00 6C 00 6C 00 6F 00<\/p>\n<p>This is the Unicode (little-endian) encoding with no BOM.\uff08\u8fd9\u662f\u4e0d\u5e26BOM\u7684\u5c0f\u7aef\u5e8fUnicode\u7f16\u7801\u3002\uff09<\/p>\n<p><span style=\"color: #0000ff;\">FF FE<\/span> 48 00 65 00 6C 00 6C 00 6F 00<\/p>\n<p>This is the Unicode (little-endian) encoding with BOM. The BOM (FF FE) serves two purposes: First, it tags the file as a Unicode document, and second, the order in which the two bytes appear indicate that the file is little-endian.<\/p>\n<p>\u8fd9\u662f\u5e26BOM\u7684\u5c0f\u7aef\u5e8fUnicode\u7f16\u7801\u3002BOM\uff08\u5373\u5f00\u5934\u7684FF FE\uff09\u7528\u9014\u6709\u4e8c\uff1a\u4e00\u6765\uff0c\u6807\u793a\u8be5\u6587\u4ef6\u4e3aUnicode\u7f16\u7801\uff1b\u4e8c\u6765\uff0c\u8fd9\u4e24\u4e2a\u5b57\u8282\u7684\u987a\u5e8f\u8868\u660e\u8fd9\u4e2a\u6587\u4ef6\u662f\u5c0f\u7aef\u5e8f\u7684\u3002<\/p>\n<p>00 48 00 65 00 6C 00 6C 00 6F<\/p>\n<p>This is the Unicode (big-endian) encoding with no BOM. Notepad does not support this encoding.<\/p>\n<p>\u8fd9\u662f\u4e0d\u5e26BOM\u7684\u5927\u7aef\u5e8fUnicode\u7f16\u7801\u3002\u8bb0\u4e8b\u672c\u4e0d\u652f\u6301\u8fd9\u79cd\u7f16\u7801\u3002<\/p>\n<p><span style=\"color: #0000ff;\">FE FF<\/span> 00 48 00 65 00 6C 00 6C 00 6F<\/p>\n<p>This is the Unicode (big-endian) encoding with BOM. Notice that this BOM is in the opposite order from the little-endian BOM.<\/p>\n<p>\u8fd9\u662f\u5e26BOM\u7684\u5927\u7aef\u5e8fUnicode\u7f16\u7801\uff0c\u6ce8\u610fBOM\u7684\u5b57\u8282\u987a\u5e8f\u4e0e\u5c0f\u7aef\u5e8fBOM\u76f8\u53cd\u3002<\/p>\n<p><span style=\"color: #0000ff;\">EF BB BF<\/span> 48 65 6C 6C 6F<\/p>\n<p>This is UTF-8 encoding. The first three bytes are the UTF-8 encoding of the BOM.<\/p>\n<p>\u8fd9\u662fUTF-8\u7f16\u7801\uff0c\u5f00\u5934\u7684\u4e09\u4e2a\u5b57\u8282\u662fUTF-8\u7f16\u7801\u7684BOM\u3002<\/p>\n<p><span style=\"color: #0000ff;\">2B 2F 76 38 2D<\/span> 48 65 6C 6C 6F<\/p>\n<p>This is UTF-7 encoding. The first five bytes are the UTF-7 encoding of the BOM. Notepad doesn&#8217;t support this encoding.<\/p>\n<p>\u8fd9\u662fUTF-7\u7f16\u7801\uff0c\u5f00\u5934\u7684\u4e94\u4e2a\u5b57\u8282\u662fUTF-7\u7f16\u7801\u7684BOM\uff0c\u8bb0\u4e8b\u672c\u4e0d\u652f\u6301\u8fd9\u79cd\u7f16\u7801\u3002<\/p>\n<p>Notice that the UTF7 BOM encoding is just the ASCII string &#8220;+\/v8-&#8220;, which is difficult to distinguish from just a regular file that happens to begin with those five characters (as odd as they may be).<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0cUTF-7\u7684BOM\u5934\u7684\u7f16\u7801\u6b63\u597d\u662fASCII\u5b57\u7b26\u4e32\u300e+\/v8-\u300f\uff0c\u5982\u679c\u6587\u672c\u6587\u4ef6\u6b63\u597d\u4ee5\u8fd9\u4e94\u4e2a\u5b57\u7b26\u5f00\u5934\uff0c\u5bf9\u731c\u6d4b\u5176\u7f16\u7801\u4f1a\u9020\u6210\u4e00\u5b9a\u7684\u56f0\u96be\uff08\u867d\u7136\u4ee5\u8fd9\u4e94\u4e2a\u5b57\u7b26\u5f00\u5934\u672c\u8eab\u5c31\u6709\u70b9\u602a\u602a\u7684\u4e86\uff09\u3002<\/p>\n<p>The encodings that do not have special prefixes and which are still supported by Notepad are the traditional ANSI encoding (i.e., &#8220;plain ASCII&#8221;) and the Unicode (little-endian) encoding with no BOM. When faced with a file that lacks a special prefix, Notepad is forced to guess which of those two encodings the file actually uses. The function that does this work is IsTextUnicode, which studies a chunk of bytes and does some statistical analysis to come up with a guess.<\/p>\n<p>\u4e0d\u5305\u542b\u4efb\u4f55\u7279\u6b8a\u7684\u524d\u7f00\u3001\u4f46\u4ecd\u88ab\u8bb0\u4e8b\u672c\u652f\u6301\u7684\u7f16\u7801\u662f\u4f20\u7edf\u7684ANSI\u7f16\u7801\uff08\u4ea6\u5373\u6240\u8c13\u7684\u7eafASCII\uff09\u548c\u4e0d\u5e26BOM\u7684\u5c0f\u7aef\u5e8fUnicode\u7f16\u7801\u3002\u5f53\u9762\u5bf9\u6ca1\u6709\u7279\u6b8a\u524d\u7f00\u7684\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u8bb0\u4e8b\u672c\u5c06\u88ab\u8feb\u731c\u6d4b\u6587\u4ef6\u5b9e\u9645\u4f7f\u7528\u7684\u7f16\u7801\u3002\u7528\u4ee5\u5904\u7406\u8fd9\u9879\u4e1a\u52a1\u7684\u51fd\u6570\u53ebIsTextUnicode\uff0c\u901a\u8fc7\u5bf9\u4e00\u5757\u5b57\u8282\u8fdb\u884c\u7814\u7a76\u3001\u5e76\u8fdb\u884c\u67d0\u4e9b\u7edf\u8ba1\u6027\u5206\u6790\u6765\u5bf9\u6587\u4ef6\u7684\u7f16\u7801\u8fdb\u884c\u731c\u6d4b\u3002<\/p>\n<p>And as the documentation notes, &#8220;Absolute certainty is not guaranteed.&#8221; Short strings are most likely to be misdetected.<\/p>\n<p>\u5e76\u4e14\u8fd9\u4e2a\u51fd\u6570\u7684\u6587\u6863\u4ea6\u6709\u6ce8\u660e\u300e\u65e0\u6cd5\u4fdd\u8bc1\u5bf9\u7f16\u7801\u7edd\u5bf9\u51c6\u786e\u7684\u731c\u6d4b\u300f\u3002\u77ed\u5c0f\u7684\u5b57\u7b26\u4e32\u88ab\u731c\u9519\u7684\u51e0\u7387\u76f8\u5bf9\u4f1a\u6bd4\u8f83\u5927\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8054\u901a\u7535\u6c60\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2670","post","type-post","status-publish","format-standard","hentry","category-tont_history"],"_links":{"self":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/2670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/comments?post=2670"}],"version-history":[{"count":0,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/2670\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/media?parent=2670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/categories?post=2670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/tags?post=2670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}