{"id":308,"date":"2019-01-26T21:53:32","date_gmt":"2019-01-26T13:53:32","guid":{"rendered":"http:\/\/www.yumefx.com\/?p=308"},"modified":"2021-03-04T16:00:51","modified_gmt":"2021-03-04T08:00:51","slug":"%e5%85%b3%e4%ba%8e%e5%ad%97%e7%ac%a6%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.yumefx.com\/?p=308","title":{"rendered":"\u5173\u4e8e\u5b57\u7b26\u7f16\u7801"},"content":{"rendered":"\n<h1 style=\"text-align: center;\"><strong>ASCII \u7801<\/strong><\/h1>\n<p>\u6211\u4eec\u77e5\u9053\uff0c\u8ba1\u7b97\u673a\u5185\u90e8\uff0c\u6240\u6709\u4fe1\u606f\u6700\u7ec8\u90fd\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u503c\u3002\u6bcf\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff08bit\uff09\u6709<code>0<\/code>\u548c<code>1<\/code>\u4e24\u79cd\u72b6\u6001\uff0c\u56e0\u6b64\u516b\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5c31\u53ef\u4ee5\u7ec4\u5408\u51fa256\u79cd\u72b6\u6001\uff0c\u8fd9\u88ab\u79f0\u4e3a\u4e00\u4e2a\u5b57\u8282\uff08byte\uff09\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2a\u5b57\u8282\u4e00\u5171\u53ef\u4ee5\u7528\u6765\u8868\u793a256\u79cd\u4e0d\u540c\u7684\u72b6\u6001\uff0c\u6bcf\u4e00\u4e2a\u72b6\u6001\u5bf9\u5e94\u4e00\u4e2a\u7b26\u53f7\uff0c\u5c31\u662f256\u4e2a\u7b26\u53f7\uff0c\u4ece<code>00000000<\/code>\u5230<code>11111111<\/code>\u3002<\/p>\n<p>\u4e0a\u4e2a\u4e16\u7eaa60\u5e74\u4ee3\uff0c\u7f8e\u56fd\u5236\u5b9a\u4e86\u4e00\u5957\u5b57\u7b26\u7f16\u7801\uff0c\u5bf9\u82f1\u8bed\u5b57\u7b26\u4e0e\u4e8c\u8fdb\u5236\u4f4d\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u505a\u4e86\u7edf\u4e00\u89c4\u5b9a\u3002\u8fd9\u88ab\u79f0\u4e3a ASCII \u7801\uff0c\u4e00\u76f4\u6cbf\u7528\u81f3\u4eca\u3002<\/p>\n<p>ASCII \u7801\u4e00\u5171\u89c4\u5b9a\u4e86128\u4e2a\u5b57\u7b26\u7684\u7f16\u7801\uff0c\u6bd4\u5982\u7a7a\u683c<code>SPACE<\/code>\u662f32\uff08\u4e8c\u8fdb\u5236<code>00100000<\/code>\uff09\uff0c\u5927\u5199\u7684\u5b57\u6bcd<code>A<\/code>\u662f65\uff08\u4e8c\u8fdb\u5236<code>01000001<\/code>\uff09\u3002<\/p>\n<p>\u8fd9128\u4e2a\u7b26\u53f7\uff08\u5305\u62ec32\u4e2a\u4e0d\u80fd\u6253\u5370\u51fa\u6765\u7684\u63a7\u5236\u7b26\u53f7\uff09\uff0c\u53ea\u5360\u7528\u4e86\u4e00\u4e2a\u5b57\u8282\u7684\u540e\u97627\u4f4d\uff0c\u6700\u524d\u9762\u7684\u4e00\u4f4d\u7edf\u4e00\u89c4\u5b9a\u4e3a<code>0<\/code>\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\"><strong>\u975e ASCII \u7f16\u7801<\/strong><\/h1>\n<p>\u82f1\u8bed\u7528128\u4e2a\u7b26\u53f7\u7f16\u7801\u5c31\u591f\u4e86\uff0c\u4f46\u662f\u7528\u6765\u8868\u793a\u5176\u4ed6\u8bed\u8a00\uff0c128\u4e2a\u7b26\u53f7\u662f\u4e0d\u591f\u7684\u3002\u6bd4\u5982\uff0c\u5728\u6cd5\u8bed\u4e2d\uff0c\u5b57\u6bcd\u4e0a\u65b9\u6709\u6ce8\u97f3\u7b26\u53f7\uff0c\u5b83\u5c31\u65e0\u6cd5\u7528 ASCII \u7801\u8868\u793a\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u4e00\u4e9b\u6b27\u6d32\u56fd\u5bb6\u5c31\u51b3\u5b9a\uff0c\u5229\u7528\u5b57\u8282\u4e2d\u95f2\u7f6e\u7684\u6700\u9ad8\u4f4d\u7f16\u5165\u65b0\u7684\u7b26\u53f7\u3002\u6bd4\u5982\uff0c\u6cd5\u8bed\u4e2d\u7684<code>\u00e9<\/code>\u7684\u7f16\u7801\u4e3a130\uff08\u4e8c\u8fdb\u5236<code>10000010<\/code>\uff09\u3002<\/p>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u8fd9\u4e9b\u6b27\u6d32\u56fd\u5bb6\u4f7f\u7528\u7684\u7f16\u7801\u4f53\u7cfb\uff0c\u53ef\u4ee5\u8868\u793a\u6700\u591a256\u4e2a\u7b26\u53f7\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u8fd9\u91cc\u53c8\u51fa\u73b0\u4e86\u65b0\u7684\u95ee\u9898\u3002\u4e0d\u540c\u7684\u56fd\u5bb6\u6709\u4e0d\u540c\u7684\u5b57\u6bcd\uff0c\u56e0\u6b64\uff0c\u54ea\u6015\u5b83\u4eec\u90fd\u4f7f\u7528256\u4e2a\u7b26\u53f7\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u4ee3\u8868\u7684\u5b57\u6bcd\u5374\u4e0d\u4e00\u6837\u3002<\/p>\n<p>\u6bd4\u5982\uff0c130\u5728\u6cd5\u8bed\u7f16\u7801\u4e2d\u4ee3\u8868\u4e86<code>\u00e9<\/code>\uff0c\u5728\u5e0c\u4f2f\u6765\u8bed\u7f16\u7801\u4e2d\u5374\u4ee3\u8868\u4e86\u5b57\u6bcd<code>Gimel<\/code>\u00a0(<code>\u05d2<\/code>)\uff0c\u5728\u4fc4\u8bed\u7f16\u7801\u4e2d\u53c8\u4f1a\u4ee3\u8868\u53e6\u4e00\u4e2a\u7b26\u53f7\u3002<\/p>\n<p>\u4f46\u662f\u4e0d\u7ba1\u600e\u6837\uff0c<strong>\u6240\u6709\u8fd9\u4e9b\u7f16\u7801\u65b9\u5f0f\u4e2d\uff0c0\u2013127\u8868\u793a\u7684\u7b26\u53f7\u662f\u4e00\u6837\u7684\uff0c\u4e0d\u4e00\u6837\u7684\u53ea\u662f128\u2013255\u7684\u8fd9\u4e00\u6bb5<\/strong>\u3002<\/p>\n<p>\u81f3\u4e8e\u4e9a\u6d32\u56fd\u5bb6\u7684\u6587\u5b57\uff0c\u4f7f\u7528\u7684\u7b26\u53f7\u5c31\u66f4\u591a\u4e86\uff0c\u6c49\u5b57\u5c31\u591a\u8fbe10\u4e07\u5de6\u53f3\u3002\u4e00\u4e2a\u5b57\u8282\u53ea\u80fd\u8868\u793a256\u79cd\u7b26\u53f7\uff0c\u80af\u5b9a\u662f\u4e0d\u591f\u7684\uff0c\u5c31\u5fc5\u987b\u4f7f\u7528\u591a\u4e2a\u5b57\u8282\u8868\u8fbe\u4e00\u4e2a\u7b26\u53f7\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u7b80\u4f53\u4e2d\u6587\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u662f GB2312\uff0c\u4f7f\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u4e2a\u6c49\u5b57\uff0c\u6240\u4ee5\u7406\u8bba\u4e0a\u6700\u591a\u53ef\u4ee5\u8868\u793a 256 x 256 = 65536 \u4e2a\u7b26\u53f7\u3002<\/p>\n<p>\u4e2d\u6587\u7f16\u7801\u7684\u95ee\u9898\u9700\u8981\u4e13\u6587\u8ba8\u8bba\uff0c\u8fd9\u7bc7\u7b14\u8bb0\u4e0d\u6d89\u53ca\u3002\u8fd9\u91cc\u53ea\u6307\u51fa\uff0c\u867d\u7136\u90fd\u662f\u7528\u591a\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u4e2a\u7b26\u53f7\uff0c\u4f46\u662fGB\u7c7b\u7684\u6c49\u5b57\u7f16\u7801\u4e0e\u540e\u6587\u7684 Unicode \u548c UTF-8 \u662f\u6beb\u65e0\u5173\u7cfb\u7684\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\"><strong>Unicode<\/strong><\/h1>\n<p>\u6b63\u5982\u4e0a\u4e00\u8282\u6240\u8bf4\uff0c\u4e16\u754c\u4e0a\u5b58\u5728\u7740\u591a\u79cd\u7f16\u7801\u65b9\u5f0f\uff0c\u540c\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u5b57\u53ef\u4ee5\u88ab\u89e3\u91ca\u6210\u4e0d\u540c\u7684\u7b26\u53f7\u3002\u56e0\u6b64\uff0c\u8981\u60f3\u6253\u5f00\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u5c31\u5fc5\u987b\u77e5\u9053\u5b83\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u5426\u5219\u7528\u9519\u8bef\u7684\u7f16\u7801\u65b9\u5f0f\u89e3\u8bfb\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u7535\u5b50\u90ae\u4ef6\u5e38\u5e38\u51fa\u73b0\u4e71\u7801\uff1f\u5c31\u662f\u56e0\u4e3a\u53d1\u4fe1\u4eba\u548c\u6536\u4fe1\u4eba\u4f7f\u7528\u7684\u7f16\u7801\u65b9\u5f0f\u4e0d\u4e00\u6837\u3002<\/p>\n<p>\u53ef\u4ee5\u60f3\u8c61\uff0c\u5982\u679c\u6709\u4e00\u79cd\u7f16\u7801\uff0c\u5c06\u4e16\u754c\u4e0a\u6240\u6709\u7684\u7b26\u53f7\u90fd\u7eb3\u5165\u5176\u4e2d\u3002\u6bcf\u4e00\u4e2a\u7b26\u53f7\u90fd\u7ed9\u4e88\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684\u7f16\u7801\uff0c\u90a3\u4e48\u4e71\u7801\u95ee\u9898\u5c31\u4f1a\u6d88\u5931\u3002<\/p>\n<p><strong>\u8fd9\u5c31\u662f Unicode\uff0c\u5c31\u50cf\u5b83\u7684\u540d\u5b57\u90fd\u8868\u793a\u7684\uff0c\u8fd9\u662f\u4e00\u79cd\u6240\u6709\u7b26\u53f7\u7684\u7f16\u7801\u3002<\/strong><\/p>\n<p>Unicode \u5f53\u7136\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u96c6\u5408\uff0c\u73b0\u5728\u7684\u89c4\u6a21\u53ef\u4ee5\u5bb9\u7eb3100\u591a\u4e07\u4e2a\u7b26\u53f7\u3002<\/p>\n<p>\u6bcf\u4e2a\u7b26\u53f7\u7684\u7f16\u7801\u90fd\u4e0d\u4e00\u6837\uff0c\u6bd4\u5982\uff0c<code>U+0639<\/code>\u8868\u793a\u963f\u62c9\u4f2f\u5b57\u6bcd<code>Ain<\/code>\uff0c<code>U+0041<\/code>\u8868\u793a\u82f1\u8bed\u7684\u5927\u5199\u5b57\u6bcd<code>A<\/code>\uff0c<code>U+4E25<\/code>\u8868\u793a\u6c49\u5b57<code>\u4e25<\/code>\u3002<\/p>\n<p>\u5177\u4f53\u7684\u7b26\u53f7\u5bf9\u5e94\u8868\uff0c\u53ef\u4ee5\u67e5\u8be2<a href=\"https:\/\/www.unicode.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">unicode.org<\/a>\uff0c\u6216\u8005\u4e13\u95e8\u7684<a href=\"http:\/\/www.chi2ko.com\/tool\/CJK.htm\" target=\"_blank\" rel=\"noreferrer noopener\">\u6c49\u5b57\u5bf9\u5e94\u8868<\/a>\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\"><strong>Unicode \u7684\u95ee\u9898<\/strong><\/h1>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cUnicode \u53ea\u662f\u4e00\u4e2a\u7b26\u53f7\u96c6\uff0c\u5b83\u53ea\u89c4\u5b9a\u4e86\u7b26\u53f7\u7684\u4e8c\u8fdb\u5236\u4ee3\u7801\uff0c\u5374\u6ca1\u6709\u89c4\u5b9a\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u4ee3\u7801\u5e94\u8be5\u5982\u4f55\u5b58\u50a8\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u6c49\u5b57<code>\u4e25<\/code>\u7684 Unicode \u662f\u5341\u516d\u8fdb\u5236\u6570<code>4E25<\/code>\uff0c\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u6570\u8db3\u8db3\u670915\u4f4d\uff08<code>100111000100101<\/code>\uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u4e2a\u7b26\u53f7\u7684\u8868\u793a\u81f3\u5c11\u9700\u89812\u4e2a\u5b57\u8282\u3002<\/p>\n<p>\u8868\u793a\u5176\u4ed6\u66f4\u5927\u7684\u7b26\u53f7\uff0c\u53ef\u80fd\u9700\u89813\u4e2a\u5b57\u8282\u6216\u80054\u4e2a\u5b57\u8282\uff0c\u751a\u81f3\u66f4\u591a\u3002<\/p>\n<p>\u8fd9\u91cc\u5c31\u6709\u4e24\u4e2a\u4e25\u91cd\u7684\u95ee\u9898\uff0c\u7b2c\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u5982\u4f55\u624d\u80fd\u533a\u522b Unicode \u548c ASCII \uff1f\u8ba1\u7b97\u673a\u600e\u4e48\u77e5\u9053\u4e09\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u4e2a\u7b26\u53f7\uff0c\u800c\u4e0d\u662f\u5206\u522b\u8868\u793a\u4e09\u4e2a\u7b26\u53f7\u5462\uff1f<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u95ee\u9898\u662f\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\uff0c\u82f1\u6587\u5b57\u6bcd\u53ea\u7528\u4e00\u4e2a\u5b57\u8282\u8868\u793a\u5c31\u591f\u4e86\uff0c\u5982\u679c Unicode \u7edf\u4e00\u89c4\u5b9a\uff0c\u6bcf\u4e2a\u7b26\u53f7\u7528\u4e09\u4e2a\u6216\u56db\u4e2a\u5b57\u8282\u8868\u793a\uff0c\u90a3\u4e48\u6bcf\u4e2a\u82f1\u6587\u5b57\u6bcd\u524d\u90fd\u5fc5\u7136\u6709\u4e8c\u5230\u4e09\u4e2a\u5b57\u8282\u662f<code>0<\/code>\uff0c\u8fd9\u5bf9\u4e8e\u5b58\u50a8\u6765\u8bf4\u662f\u6781\u5927\u7684\u6d6a\u8d39\uff0c\u6587\u672c\u6587\u4ef6\u7684\u5927\u5c0f\u4f1a\u56e0\u6b64\u5927\u51fa\u4e8c\u4e09\u500d\uff0c\u8fd9\u662f\u65e0\u6cd5\u63a5\u53d7\u7684\u3002<\/p>\n<p>\u5b83\u4eec\u9020\u6210\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n<p>1\uff09\u51fa\u73b0\u4e86 Unicode \u7684\u591a\u79cd\u5b58\u50a8\u65b9\u5f0f\uff0c\u4e5f\u5c31\u662f\u8bf4\u6709\u8bb8\u591a\u79cd\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u8868\u793a Unicode\u3002<\/p>\n<p>2\uff09Unicode \u5728\u5f88\u957f\u4e00\u6bb5\u65f6\u95f4\u5185\u65e0\u6cd5\u63a8\u5e7f\uff0c\u76f4\u5230\u4e92\u8054\u7f51\u7684\u51fa\u73b0\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\"><strong>UTF-8<\/strong><\/h1>\n<p>\u4e92\u8054\u7f51\u7684\u666e\u53ca\uff0c\u5f3a\u70c8\u8981\u6c42\u51fa\u73b0\u4e00\u79cd\u7edf\u4e00\u7684\u7f16\u7801\u65b9\u5f0f\u3002UTF-8 \u5c31\u662f\u5728\u4e92\u8054\u7f51\u4e0a\u4f7f\u7528\u6700\u5e7f\u7684\u4e00\u79cd Unicode \u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u5176\u4ed6\u5b9e\u73b0\u65b9\u5f0f\u8fd8\u5305\u62ec UTF-16\uff08\u5b57\u7b26\u7528\u4e24\u4e2a\u5b57\u8282\u6216\u56db\u4e2a\u5b57\u8282\u8868\u793a\uff09\u548c UTF-32\uff08\u5b57\u7b26\u7528\u56db\u4e2a\u5b57\u8282\u8868\u793a\uff09\uff0c\u4e0d\u8fc7\u5728\u4e92\u8054\u7f51\u4e0a\u57fa\u672c\u4e0d\u7528\u3002<\/p>\n<p class=\"has-text-color has-vivid-red-color\"><strong>\u91cd\u590d\u4e00\u904d\uff0c\u8fd9\u91cc\u7684\u5173\u7cfb\u662f\uff0cUTF-8 \u662f Unicode \u7684\u5b9e\u73b0\u65b9\u5f0f\u4e4b\u4e00\u3002<\/strong><\/p>\n<p>UTF-8 \u6700\u5927\u7684\u4e00\u4e2a\u7279\u70b9\uff0c\u5c31\u662f\u5b83\u662f\u4e00\u79cd\u53d8\u957f\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u5b83\u53ef\u4ee5\u4f7f\u75281~4\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u4e2a\u7b26\u53f7\uff0c\u6839\u636e\u4e0d\u540c\u7684\u7b26\u53f7\u800c\u53d8\u5316\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n<p>UTF-8 \u7684\u7f16\u7801\u89c4\u5219\u5f88\u7b80\u5355\uff0c\u53ea\u6709\u4e8c\u6761\uff1a<\/p>\n<p>1\uff09\u5bf9\u4e8e\u5355\u5b57\u8282\u7684\u7b26\u53f7\uff0c\u5b57\u8282\u7684\u7b2c\u4e00\u4f4d\u8bbe\u4e3a<code>0<\/code>\uff0c\u540e\u97627\u4f4d\u4e3a\u8fd9\u4e2a\u7b26\u53f7\u7684 Unicode \u7801\u3002\u56e0\u6b64\u5bf9\u4e8e\u82f1\u8bed\u5b57\u6bcd\uff0cUTF-8 \u7f16\u7801\u548c ASCII \u7801\u662f\u76f8\u540c\u7684\u3002<\/p>\n<p>2\uff09\u5bf9\u4e8e<code>n<\/code>\u5b57\u8282\u7684\u7b26\u53f7\uff08<code>n &gt; 1<\/code>\uff09\uff0c\u7b2c\u4e00\u4e2a\u5b57\u8282\u7684\u524d<code>n<\/code>\u4f4d\u90fd\u8bbe\u4e3a<code>1<\/code>\uff0c\u7b2c<code>n + 1<\/code>\u4f4d\u8bbe\u4e3a<code>0<\/code>\uff0c\u540e\u9762\u5b57\u8282\u7684\u524d\u4e24\u4f4d\u4e00\u5f8b\u8bbe\u4e3a<code>10<\/code>\u3002\u5269\u4e0b\u7684\u6ca1\u6709\u63d0\u53ca\u7684\u4e8c\u8fdb\u5236\u4f4d\uff0c\u5168\u90e8\u4e3a\u8fd9\u4e2a\u7b26\u53f7\u7684 Unicode \u7801\u3002<\/p>\n<p>\u4e0b\u8868\u603b\u7ed3\u4e86\u7f16\u7801\u89c4\u5219\uff0c\u5b57\u6bcd<code>x<\/code>\u8868\u793a\u53ef\u7528\u7f16\u7801\u7684\u4f4d\u3002<\/p>\n\n\n\n<pre>\n<span>Unicode\u7b26\u53f7\u8303\u56f4     |        UTF-8\u7f16\u7801\u65b9\u5f0f<\/span>\n<span>(\u5341\u516d\u8fdb\u5236)        |              \uff08\u4e8c\u8fdb\u5236\uff09<\/span>\n<span>----------------------+---------------------------------------------<\/span>\n<span>0000 0000-0000 007F | 0xxxxxxx<\/span>\n<span>0000 0080-0000 07FF | 110xxxxx 10xxxxxx<\/span>\n<span>0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx<\/span>\n<span>0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx<\/span>\n<\/pre>\n\n\n\n<p>\u8ddf\u636e\u4e0a\u8868\uff0c\u89e3\u8bfb UTF-8 \u7f16\u7801\u975e\u5e38\u7b80\u5355\u3002\u5982\u679c\u4e00\u4e2a\u5b57\u8282\u7684\u7b2c\u4e00\u4f4d\u662f<code>0<\/code>\uff0c\u5219\u8fd9\u4e2a\u5b57\u8282\u5355\u72ec\u5c31\u662f\u4e00\u4e2a\u5b57\u7b26\uff1b\u5982\u679c\u7b2c\u4e00\u4f4d\u662f<code>1<\/code>\uff0c\u5219\u8fde\u7eed\u6709\u591a\u5c11\u4e2a<code>1<\/code>\uff0c\u5c31\u8868\u793a\u5f53\u524d\u5b57\u7b26\u5360\u7528\u591a\u5c11\u4e2a\u5b57\u8282\u3002<\/p>\n<p>\u4e0b\u9762\uff0c\u8fd8\u662f\u4ee5\u6c49\u5b57<code>\u4e25<\/code>\u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5b9e\u73b0 UTF-8 \u7f16\u7801\u3002<\/p>\n<p><code>\u4e25<\/code>\u7684 Unicode \u662f<code>4E25<\/code>\uff08<code>100111000100101<\/code>\uff09\uff0c\u6839\u636e\u4e0a\u8868\uff0c\u53ef\u4ee5\u53d1\u73b0<code>4E25<\/code>\u5904\u5728\u7b2c\u4e09\u884c\u7684\u8303\u56f4\u5185\uff08<code>0000 0800 - 0000 FFFF<\/code>\uff09\uff0c\u56e0\u6b64<code>\u4e25<\/code>\u7684 UTF-8 \u7f16\u7801\u9700\u8981\u4e09\u4e2a\u5b57\u8282\uff0c\u5373\u683c\u5f0f\u662f<code>1110xxxx 10xxxxxx 10xxxxxx<\/code>\u3002<\/p>\n<p>\u7136\u540e\uff0c\u4ece<code>\u4e25<\/code>\u7684\u6700\u540e\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5f00\u59cb\uff0c\u4f9d\u6b21\u4ece\u540e\u5411\u524d\u586b\u5165\u683c\u5f0f\u4e2d\u7684<code>x<\/code>\uff0c\u591a\u51fa\u7684\u4f4d\u8865<code>0<\/code>\u3002\u8fd9\u6837\u5c31\u5f97\u5230\u4e86\uff0c<code>\u4e25<\/code>\u7684 UTF-8 \u7f16\u7801\u662f<code>11100100 10111000 10100101<\/code>\uff0c\u8f6c\u6362\u6210\u5341\u516d\u8fdb\u5236\u5c31\u662f<code>E4B8A5<\/code>\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\"><strong>Unicode \u4e0e UTF-8 \u4e4b\u95f4\u7684\u8f6c\u6362<\/strong><\/h1>\n<p>\u901a\u8fc7\u4e0a\u4e00\u8282\u7684\u4f8b\u5b50\uff0c\u53ef\u4ee5\u770b\u5230<code>\u4e25<\/code>\u7684 Unicode\u7801 \u662f<code>4E25<\/code>\uff0cUTF-8 \u7f16\u7801\u662f<code>E4B8A5<\/code>\uff0c\u4e24\u8005\u662f\u4e0d\u4e00\u6837\u7684\u3002\u5b83\u4eec\u4e4b\u95f4\u7684\u8f6c\u6362\u53ef\u4ee5\u901a\u8fc7\u7a0b\u5e8f\u5b9e\u73b0\u3002<\/p>\n<p>Windows\u5e73\u53f0\uff0c\u6709\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u8f6c\u5316\u65b9\u6cd5\uff0c\u5c31\u662f\u4f7f\u7528\u5185\u7f6e\u7684\u8bb0\u4e8b\u672c\u5c0f\u7a0b\u5e8f<code>notepad.exe<\/code>\u3002<\/p>\n<p>\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u70b9\u51fb<code>\u6587\u4ef6<\/code>\u83dc\u5355\u4e2d\u7684<code>\u53e6\u5b58\u4e3a<\/code>\u547d\u4ee4\uff0c\u4f1a\u8df3\u51fa\u4e00\u4e2a\u5bf9\u8bdd\u6846\uff0c\u5728\u6700\u5e95\u90e8\u6709\u4e00\u4e2a<code>\u7f16\u7801<\/code>\u7684\u4e0b\u62c9\u6761\u3002<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-916 aligncenter\" src=\"http:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/notepadSave.png\" alt=\"\" width=\"671\" height=\"243\" srcset=\"https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/notepadSave.png 671w, https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/notepadSave-300x109.png 300w\" sizes=\"(max-width: 671px) 100vw, 671px\" \/><\/p>\n<p>\u91cc\u9762\u6709\u56db\u4e2a\u9009\u9879\uff1a<code>ANSI<\/code>\uff0c<code>Unicode<\/code>\uff0c<code>Unicode big endian<\/code>\u548c<code>UTF-8<\/code>\u3002<\/p>\n<p>1\uff09<code>ANSI<\/code>\u662f\u9ed8\u8ba4\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u5bf9\u4e8e\u82f1\u6587\u6587\u4ef6\u662f<code>ASCII<\/code>\u7f16\u7801\uff0c\u5bf9\u4e8e\u7b80\u4f53\u4e2d\u6587\u6587\u4ef6\u662f<code>GB2312<\/code>\u7f16\u7801\uff08\u53ea\u9488\u5bf9 Windows \u7b80\u4f53\u4e2d\u6587\u7248\uff0c\u5982\u679c\u662f\u7e41\u4f53\u4e2d\u6587\u7248\u4f1a\u91c7\u7528 Big5 \u7801\uff09\u3002<\/p>\n<p>2\uff09<code>Unicode<\/code>\u7f16\u7801\u8fd9\u91cc\u6307\u7684\u662f<code>notepad.exe<\/code>\u4f7f\u7528\u7684 UCS-2 \u7f16\u7801\u65b9\u5f0f\uff0c\u5373\u76f4\u63a5\u7528\u4e24\u4e2a\u5b57\u8282\u5b58\u5165\u5b57\u7b26\u7684 Unicode \u7801\uff0c\u8fd9\u4e2a\u9009\u9879\u7528\u7684 little endian \u683c\u5f0f\u3002<\/p>\n<p>3\uff09<code>Unicode big endian<\/code>\u7f16\u7801\u4e0e\u4e0a\u4e00\u4e2a\u9009\u9879\u76f8\u5bf9\u5e94\u3002\u6211\u5728\u4e0b\u4e00\u8282\u4f1a\u89e3\u91ca little endian \u548c big endian \u7684\u6db5\u4e49\u3002<\/p>\n<p>4\uff09<code>UTF-8<\/code>\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u4e0a\u4e00\u8282\u8c08\u5230\u7684\u7f16\u7801\u65b9\u6cd5\u3002<\/p>\n<p>\u9009\u62e9\u5b8c\u201d\u7f16\u7801\u65b9\u5f0f\u201d\u540e\uff0c\u70b9\u51fb\u201d\u4fdd\u5b58\u201d\u6309\u94ae\uff0c\u6587\u4ef6\u7684\u7f16\u7801\u65b9\u5f0f\u5c31\u7acb\u523b\u8f6c\u6362\u597d\u4e86\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\"><strong>Little endian \u548c Big endian<\/strong><\/h1>\n<p>\u4e0a\u4e00\u8282\u5df2\u7ecf\u63d0\u5230\uff0cUCS-2 \u683c\u5f0f\u53ef\u4ee5\u5b58\u50a8 Unicode \u7801\uff08\u7801\u70b9\u4e0d\u8d85\u8fc7<code>0xFFFF<\/code>\uff09\u3002\u4ee5\u6c49\u5b57<code>\u4e25<\/code>\u4e3a\u4f8b\uff0cUnicode \u7801\u662f<code>4E25<\/code>\uff0c\u9700\u8981\u7528\u4e24\u4e2a\u5b57\u8282\u5b58\u50a8\uff0c\u4e00\u4e2a\u5b57\u8282\u662f<code>4E<\/code>\uff0c\u53e6\u4e00\u4e2a\u5b57\u8282\u662f<code>25<\/code>\u3002<\/p>\n<p>\u5b58\u50a8\u7684\u65f6\u5019\uff0c<code>4E<\/code>\u5728\u524d\uff0c<code>25<\/code>\u5728\u540e\uff0c\u8fd9\u5c31\u662f Big endian \u65b9\u5f0f\uff1b<code>25<\/code>\u5728\u524d\uff0c<code>4E<\/code>\u5728\u540e\uff0c\u8fd9\u662f Little endian \u65b9\u5f0f\u3002<\/p>\n<p>\u8fd9\u4e24\u4e2a\u53e4\u602a\u7684\u540d\u79f0\u6765\u81ea\u82f1\u56fd\u4f5c\u5bb6\u65af\u5a01\u592b\u7279\u7684\u300a\u683c\u5217\u4f5b\u6e38\u8bb0\u300b\u3002\u5728\u8be5\u4e66\u4e2d\uff0c\u5c0f\u4eba\u56fd\u91cc\u7206\u53d1\u4e86\u5185\u6218\uff0c\u6218\u4e89\u8d77\u56e0\u662f\u4eba\u4eec\u4e89\u8bba\uff0c\u5403\u9e21\u86cb\u65f6\u7a76\u7adf\u662f\u4ece\u5927\u5934(Big-endian)\u6572\u5f00\u8fd8\u662f\u4ece\u5c0f\u5934(Little-endian)\u6572\u5f00\u3002\u4e3a\u4e86\u8fd9\u4ef6\u4e8b\u60c5\uff0c\u524d\u540e\u7206\u53d1\u4e86\u516d\u6b21\u6218\u4e89\uff0c\u4e00\u4e2a\u7687\u5e1d\u9001\u4e86\u547d\uff0c\u53e6\u4e00\u4e2a\u7687\u5e1d\u4e22\u4e86\u738b\u4f4d\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u5b57\u8282\u5728\u524d\uff0c\u5c31\u662f\u201d\u5927\u5934\u65b9\u5f0f\u201d\uff08Big endian\uff09\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u8282\u5728\u524d\u5c31\u662f\u201d\u5c0f\u5934\u65b9\u5f0f\u201d\uff08Little endian\uff09\u3002<\/p>\n<p>\u90a3\u4e48\u5f88\u81ea\u7136\u7684\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u8ba1\u7b97\u673a\u600e\u4e48\u77e5\u9053\u67d0\u4e00\u4e2a\u6587\u4ef6\u5230\u5e95\u91c7\u7528\u54ea\u4e00\u79cd\u65b9\u5f0f\u7f16\u7801\uff1f<\/p>\n<p>Unicode \u89c4\u8303\u5b9a\u4e49\uff0c\u6bcf\u4e00\u4e2a\u6587\u4ef6\u7684\u6700\u524d\u9762\u5206\u522b\u52a0\u5165\u4e00\u4e2a\u8868\u793a\u7f16\u7801\u987a\u5e8f\u7684\u5b57\u7b26\uff0c\u8fd9\u4e2a\u5b57\u7b26\u7684\u540d\u5b57\u53eb\u505a\u201d\u96f6\u5bbd\u5ea6\u975e\u6362\u884c\u7a7a\u683c\u201d\uff08zero width no-break space\uff09\uff0c\u7528<code>FEFF<\/code>\u8868\u793a\u3002\u8fd9\u6b63\u597d\u662f\u4e24\u4e2a\u5b57\u8282\uff0c\u800c\u4e14<code>FF<\/code>\u6bd4<code>FE<\/code>\u5927<code>1<\/code>\u3002<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u5934\u4e24\u4e2a\u5b57\u8282\u662f<code>FE FF<\/code>\uff0c\u5c31\u8868\u793a\u8be5\u6587\u4ef6\u91c7\u7528\u5927\u5934\u65b9\u5f0f\uff1b\u5982\u679c\u5934\u4e24\u4e2a\u5b57\u8282\u662f<code>FF FE<\/code>\uff0c\u5c31\u8868\u793a\u8be5\u6587\u4ef6\u91c7\u7528\u5c0f\u5934\u65b9\u5f0f\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\"><strong>\u5b9e\u4f8b<\/strong><\/h1>\n<p>\u4e0b\u9762\uff0c\u4e3e\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<p>\u6253\u5f00\u201d\u8bb0\u4e8b\u672c\u201d\u7a0b\u5e8f<code>notepad.exe<\/code>\uff0c\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u5185\u5bb9\u5c31\u662f\u4e00\u4e2a<code>\u4e25<\/code>\u5b57\uff0c\u4f9d\u6b21\u91c7\u7528<code>ANSI<\/code>\uff0c<code>Unicode<\/code>\uff0c<code>Unicode big endian<\/code>\u548c<code>UTF-8<\/code>\u7f16\u7801\u65b9\u5f0f\u4fdd\u5b58\u3002<\/p>\n<p>\u7136\u540e\uff0c\u7528\u6587\u672c\u7f16\u8f91\u8f6f\u4ef6UltraEdit \u4e2d\u7684\u201d\u5341\u516d\u8fdb\u5236\u529f\u80fd\u201d\uff0c\u89c2\u5bdf\u8be5\u6587\u4ef6\u7684\u5185\u90e8\u7f16\u7801\u65b9\u5f0f\u3002<\/p>\n<p>1\uff09ANSI\uff1a\u6587\u4ef6\u7684\u7f16\u7801\u5c31\u662f\u4e24\u4e2a\u5b57\u8282<code>D1 CF<\/code>\uff0c\u8fd9\u6b63\u662f<code>\u4e25<\/code>\u7684 GB2312 \u7f16\u7801\uff0c\u8fd9\u4e5f\u6697\u793a GB2312 \u662f\u91c7\u7528\u5927\u5934\u65b9\u5f0f\u5b58\u50a8\u7684\u3002<\/p>\n<p>2\uff09Unicode\uff1a\u7f16\u7801\u662f\u56db\u4e2a\u5b57\u8282<code>FF FE 25 4E<\/code>\uff0c\u5176\u4e2d<code>FF FE<\/code>\u8868\u660e\u662f\u5c0f\u5934\u65b9\u5f0f\u5b58\u50a8\uff0c\u771f\u6b63\u7684\u7f16\u7801\u662f<code>4E25<\/code>\u3002<\/p>\n<p>3\uff09Unicode big endian\uff1a\u7f16\u7801\u662f\u56db\u4e2a\u5b57\u8282<code>FE FF 4E 25<\/code>\uff0c\u5176\u4e2d<code>FE FF<\/code>\u8868\u660e\u662f\u5927\u5934\u65b9\u5f0f\u5b58\u50a8\u3002<\/p>\n<p>4\uff09UTF-8\uff1a\u7f16\u7801\u662f\u516d\u4e2a\u5b57\u8282<code>EF BB BF E4 B8 A5<\/code>\uff0c\u524d\u4e09\u4e2a\u5b57\u8282<code>EF BB BF<\/code>\u8868\u793a\u8fd9\u662fUTF-8\u7f16\u7801\uff0c\u540e\u4e09\u4e2a<code>E4B8A5<\/code>\u5c31\u662f<code>\u4e25<\/code>\u7684\u5177\u4f53\u7f16\u7801\uff0c\u5b83\u7684\u5b58\u50a8\u987a\u5e8f\u4e0e\u7f16\u7801\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">Python2.X\u4e2d\u7684\u7f16\u7801<\/h1>\n<p>\u5f53\u4ee3\u7801\u5305\u542b\u4e2d\u6587\u65f6\uff0c\u9700\u8981\u5728py\u6587\u4ef6\u5f00\u5934\u5982\u4e0b\u65b9\u5f0f\u6307\u5b9a\u7f16\u7801\u3002<\/p>\n\n\n\n<pre>\n<span># -*- coding: UTF-8 -*- <\/span>\n<\/pre>\n\n\n\n<p>\u56e0\u4e3a\u5728python2.X\u4e2d\u9ed8\u8ba4\u662fASCII\u7f16\u7801\uff0c \u6240\u4ee5\u5982\u679c\u57282.X\u7248\u672c\u4e2d\u5982\u679c\u4f60\u4e0d\u6307\u5b9a\u7f16\u7801\u5e76\u4e14\u5728.py\u6587\u4ef6\u4e2d\u5199\u4e86\u4e00\u4e2aASCII\u7801\u4e2d\u6ca1\u6709\u7684\u5b57\u7b26\u5c31\u4f1a\u663e\u793a\u4e71\u7801\u3002<\/p>\n<p>\u4f60\u5728\u6587\u4ef6\u4e2d\u6307\u5b9a\u7f16\u7801\u4e3aUTF-8\uff0c\u4f46\u662fUTF-8\u5982\u679c\u4f60\u60f3\u8f6cGBK\u7684\u8bdd\u662f\u4e0d\u80fd\u76f4\u63a5\u8f6c\u7684\uff0c\u9700\u8981Unicode\u505a\u4e00\u4e2a\u8f6c\u63a5\u7ad9\u70b9\u3002<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-923 aligncenter\" src=\"http:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/pythonBianma.png\" alt=\"\" width=\"840\" height=\"450\" srcset=\"https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/pythonBianma.png 840w, https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/pythonBianma-300x161.png 300w, https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/pythonBianma-768x411.png 768w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><\/p>\n<p>\u4e0b\u9762\u662f\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93chardet\u5224\u65ad\u8f6c\u7801\u540e\u7684\u7f16\u7801\uff0c\u6ce8\u610f\u8fd9\u4e2a\u5e93\u52a1\u5fc5\u5347\u7ea7\u5230\u6700\u65b0\uff0c\u65e7\u7248\u672c\u6709\u65f6\u5019\u4f1a\u5224\u65ad\u7f16\u7801\u9519\u8bef\uff08\u55ef\uff0c\u6211\u4e5f\u662f\u5403\u8fc7\u5b83\u7684\u4e8f\uff09\u3002<\/p>\n\n\n\n<pre>\n<span>#!\/usr\/bin\/env python <\/span>\n<span>#-*- coding:utf-8 -*- <\/span>\n<span>#author luotianshuai <\/span>\n<span>import chardet <\/span>\n<span>tim = &#x27;\u4f60\u597d&#x27; <\/span>\n<span>print chardet.detect(tim) <\/span>\n<span>#\u5148\u89e3\u7801\u4e3aUnicode\u7f16\u7801\uff0c\u7136\u540e\u5728\u4eceUnicode\u7f16\u7801\u4e3aGBK <\/span>\n<span>new_tim = tim.decode(&#x27;UTF-8&#x27;).encode(&#x27;GBK&#x27;) <\/span>\n<span>print chardet.detect(new_tim) <\/span>\n<span>#\u7ed3\u679c <\/span>\n<span>&#x27;&#x27;&#x27; <\/span>\n<span>{&#x27;confidence&#x27;: 0.75249999999999995, &#x27;encoding&#x27;: &#x27;utf-8&#x27;} <\/span>\n<span>{&#x27;confidence&#x27;: 0.35982121203616341, &#x27;encoding&#x27;: &#x27;TIS-620&#x27;} <\/span>\n<span>&#x27;&#x27;&#x27;<\/span>\n<\/pre>\n\n\n\n<p>\u5728py2\u4e2d\uff0c\u6709\u4e24\u79cd\u5b57\u7b26\u4e32\u7c7b\u578b\uff1astr\u7c7b\u578b\u548cunicode\u7c7b\u578b\uff1b\u6ce8\u610f\uff0c\u8fd9\u4ec5\u4ec5\u662f\u4e24\u4e2a\u540d\u5b57\uff0cpython\u5b9a\u4e49\u7684\u4e24\u4e2a\u540d\u5b57\uff0c\u5173\u952e\u662f\u8fd9\u4e24\u79cd\u6570\u636e\u7c7b\u578b\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5b58\u5728\u5185\u5b58\u5730\u5740\u7684\u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<pre>\n<span>coding:utf8<\/span>\n<span>s1=&#x27;\u82d1&#x27;<\/span>\n<span>print type(s1) # <\/span>\n<span>print repr(s1) #&#x27;\\xe8\\x8b\\x91<\/span>\n<span>s2=u&#x27;\u82d1&#x27;<\/span>\n<span>print type(s2) # <\/span>\n<span>print repr(s2) # u&#x27;\\u82d1&#x27;<\/span>\n<\/pre>\n\n\n\n<p>\u5185\u7f6e\u51fd\u6570repr\u53ef\u4ee5\u5e2e\u6211\u4eec\u5728\u8fd9\u91cc\u663e\u793a\u5b58\u50a8\u5185\u5bb9\u3002\u539f\u6765\uff0cstr\u548cunicode\u5206\u522b\u5b58\u7684\u662f\u5b57\u8282\u6570\u636e\u548cunicode\u6570\u636e\u3002<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-810 aligncenter\" src=\"http:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma2.png\" alt=\"\" width=\"1882\" height=\"1040\" srcset=\"https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma2.png 1882w, https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma2-300x166.png 300w, https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma2-768x424.png 768w, https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma2-1024x566.png 1024w\" sizes=\"(max-width: 1882px) 100vw, 1882px\" \/><\/p>\n<p><strong>py2\u7f16\u7801\u7684\u7279\u8272\uff1a<\/strong><\/p>\n\n\n\n<pre>\n<span>coding:utf8<\/span>\n<span>print &#x27;\u82d1\u660a&#x27; #  \u82d1\u660a   <\/span>\n<span>print repr(&#x27;\u82d1\u660a&#x27;)#&#x27;\\xe8\\x8b\\x91\\xe6\\x98\\x8a&#x27;<\/span>\n<span>print (u&quot;hello&quot;+&quot;yuan&quot;)<\/span>\n<span>print (u&#x27;\u82d1\u660a&#x27;+&#x27;\u6700\u5e05&#x27;)   #UnicodeDecodeError: &#x27;ascii&#x27; codec can&#x27;t decode byte 0xe6<\/span>\n<span>                         # in position 0: ordinal not in range(128) <\/span>\n<\/pre>\n\n\n\n<p>Python 2 \u6084\u6084\u63a9\u76d6\u6389\u4e86 byte \u5230 unicode \u7684\u8f6c\u6362\uff0c\u53ea\u8981\u6570\u636e\u5168\u90e8\u662f ASCII \u7684\u8bdd\uff0c\u6240\u6709\u7684\u8f6c\u6362\u90fd\u662f\u6b63\u786e\u7684\uff0c\u4e00\u65e6\u4e00\u4e2a\u975e ASCII \u5b57\u7b26\u5077\u5077\u8fdb\u5165\u4f60\u7684\u7a0b\u5e8f\uff0c\u90a3\u4e48\u9ed8\u8ba4\u7684\u89e3\u7801\u5c06\u4f1a\u5931\u6548\uff0c\u4ece\u800c\u9020\u6210 UnicodeDecodeError \u7684\u9519\u8bef\u3002<\/p>\n<p>py2\u7f16\u7801\u8ba9\u7a0b\u5e8f\u5728\u5904\u7406 ASCII \u7684\u65f6\u5019\u66f4\u52a0\u7b80\u5355\u3002\u4f60\u4ed8\u51fa\u7684\u4ee3\u4ef7\u5c31\u662f\u5728\u5904\u7406\u975e ASCII \u7684\u65f6\u5019\u5c06\u4f1a\u5931\u8d25\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">Python3.X\u4e2d\u7684\u7f16\u7801<\/h1>\n<p>Python3.X \u6e90\u7801\u6587\u4ef6\u9ed8\u8ba4\u4f7f\u7528utf-8\u7f16\u7801\uff0c\u6240\u4ee5\u53ef\u4ee5\u6b63\u5e38\u89e3\u6790\u4e2d\u6587\uff0c\u65e0\u9700\u6307\u5b9a UTF-8 \u7f16\u7801\u3002<\/p>\n<p>Python3\u4e2d\u9ed8\u8ba4\u5c31\u662funicode\u7f16\u7801\u3002<\/p>\n\n\n\n<pre>\n<span>#!\/usr\/bin\/env python <\/span>\n<span>#author luotianshuai <\/span>\n<span><\/span>\n<span>tim = &#x27;\u5929\u5e05&#x27; <\/span>\n<span>#\u8f6c\u4e3aUTF-8\u7f16\u7801 <\/span>\n<span>print(tim.encode(&#x27;UTF-8&#x27;)) <\/span>\n<span>#\u8f6c\u4e3aGBK\u7f16\u7801 <\/span>\n<span>print(tim.encode(&#x27;GBK&#x27;)) <\/span>\n<span>#\u8f6c\u4e3aASCII\u7f16\u7801(\u62a5\u9519\u4e3a\u4ec0\u4e48?\u56e0\u4e3aASCII\u7801\u8868\u4e2d\u6ca1\u6709\u2018\u5929\u5e05\u2019\u8fd9\u4e2a\u5b57\u7b26\u96c6~~) <\/span>\n<span>print(tim.encode(&#x27;ASCII&#x27;)) <\/span>\n<\/pre>\n\n\n\n<p>python3 renamed the unicode type to str ,the old str type has been replaced by bytes.<\/p>\n<p>py3\u4e5f\u6709\u4e24\u79cd\u6570\u636e\u7c7b\u578b\uff1astr\u548cbytes\uff1b \u00a0str\u7c7b\u578b\u5b58unicode\u6570\u636e\uff0cbytes\u7c7b\u578b\u5b58bytes\u6570\u636e\uff0c\u4e0epy2\u6bd4\u53ea\u662f\u6362\u4e86\u4e00\u4e0b\u540d\u5b57\u800c\u5df2\u3002<\/p>\n\n\n\n<pre>\n<span>import json<\/span>\n<span><\/span>\n<span>s=&#x27;\u82d1\u660a&#x27;<\/span>\n<span>print(type(s))       #<\/span>\n<span>print(json.dumps(s)) #  &quot;\\u82d1\\u660a&quot;<\/span>\n<span><\/span>\n<span>b=s.encode(&#x27;utf8&#x27;)<\/span>\n<span>print(type(b))      # <\/span>\n<span>print(b)            # b&#x27;\\xe8\\x8b\\x91\\xe6\\x98\\x8a&#x27;<\/span>\n<span><\/span>\n<span>u=b.decode(&#x27;utf8&#x27;)<\/span>\n<span>print(type(u))       #<\/span>\n<span>print(u)             #\u82d1\u660a<\/span>\n<span>print(json.dumps(u)) #&quot;\\u82d1\\u660a&quot;<\/span>\n<span><\/span>\n<span>print(len(&#x27;\u82d1\u660a&#x27;)) # 2<\/span>\n<\/pre>\n\n\n\n<p><img loading=\"lazy\" class=\"size-full wp-image-811 aligncenter\" src=\"http:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma3.png\" alt=\"\" width=\"1816\" height=\"742\" srcset=\"https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma3.png 1816w, https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma3-300x123.png 300w, https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma3-768x314.png 768w, https:\/\/www.yumefx.com\/wp-content\/uploads\/2019\/01\/bianma3-1024x418.png 1024w\" sizes=\"(max-width: 1816px) 100vw, 1816px\" \/><\/p>\n<p>py3\u7684\u7f16\u7801\u54f2\u5b66\uff1a<\/p>\n<p>Python 3\u6700\u91cd\u8981\u7684\u65b0\u7279\u6027\u5927\u6982\u8981\u7b97\u662f\u5bf9\u6587\u672c\u548c\u4e8c\u8fdb\u5236\u6570\u636e\u4f5c\u4e86\u66f4\u4e3a\u6e05\u6670\u7684\u533a\u5206\uff0c\u4e0d\u518d\u4f1a\u5bf9bytes\u5b57\u8282\u4e32\u8fdb\u884c\u81ea\u52a8\u89e3\u7801\u3002<\/p>\n<p>\u6587\u672c\u603b\u662fUnicode\uff0c\u7531str\u7c7b\u578b\u8868\u793a\uff0c\u4e8c\u8fdb\u5236\u6570\u636e\u5219\u7531bytes\u7c7b\u578b\u8868\u793a\u3002<\/p>\n<p>Python 3\u4e0d\u4f1a\u4ee5\u4efb\u610f\u9690\u5f0f\u7684\u65b9\u5f0f\u6df7\u7528str\u548cbytes\uff0c\u6b63\u662f\u8fd9\u4f7f\u5f97\u4e24\u8005\u7684\u533a\u5206\u7279\u522b\u6e05\u6670\u3002\u4f60\u4e0d\u80fd\u62fc\u63a5\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u5305\uff0c\u4e5f\u65e0\u6cd5\u5728\u5b57\u8282\u5305\u91cc\u641c\u7d22\u5b57\u7b26\u4e32\uff08\u53cd\u4e4b\u4ea6\u7136\uff09\uff0c\u4e5f\u4e0d\u80fd\u5c06\u5b57\u7b26\u4e32\u4f20\u5165\u53c2\u6570\u4e3a\u5b57\u8282\u5305\u7684\u51fd\u6570\uff08\u53cd\u4e4b\u4ea6\u7136\uff09\u3002<\/p>\n\n\n\n<pre>\n<span>#print(&#x27;alvin&#x27;+u&#x27;yuan&#x27;)#\u5b57\u8282\u4e32\u548cunicode\u8fde\u63a5 py2:alvinyuan<\/span>\n<span>print(b&#x27;alvin&#x27;+&#x27;yuan&#x27;)#\u5b57\u8282\u4e32\u548cunicode\u8fde\u63a5 py3:\u62a5\u9519 can&#x27;t concat bytes to str<\/span>\n<\/pre>\n\n\n\n<p>\u6ce8\u610f\uff1a\u65e0\u8bbapy2\uff0c\u8fd8\u662fpy3,\u4e0e\u660e\u6587\u76f4\u63a5\u5bf9\u5e94\u7684\u5c31\u662funicode\u6570\u636e\uff0c\u6253\u5370unicode\u6570\u636e\u5c31\u4f1a\u663e\u793a\u76f8\u5e94\u7684\u660e\u6587(\u5305\u62ec\u82f1\u6587\u548c\u4e2d\u6587) \u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u5176\u4ed6<\/h1>\n<p>win\u7684\u64cd\u4f5c\u7cfb\u7edf\u5b89\u88c5\u65f6\u662f\u9ed8\u8ba4\u7684gbk\u7f16\u7801\uff0c\u800clinux\u64cd\u4f5c\u7cfb\u7edf\u9ed8\u8ba4\u7684\u662futf8\u7f16\u7801 \u3002<\/p>\n<p>win\u4e0b\u8c03\u7528open\u51fd\u6570\u6253\u5f00\u4e00\u4e2autf-8\u7f16\u7801\u4fdd\u5b58\u7684\u6587\u4ef6\u65f6\uff0c \u8c03\u7528\u7684\u662f\u64cd\u4f5c\u7cfb\u7edf\u6253\u5f00\u6587\u4ef6 \uff0c\u56e0\u6b64\u5fc5\u987b\u6307\u5b9aencoding=\u2019utf8\u2019\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u4e71\u7801\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff0chello.py:<\/p>\n\n\n\n<pre>\n<span>#coding:utf8<\/span>\n<span>print (&#x27;\u82d1\u660a&#x27;)<\/span>\n<\/pre>\n\n\n\n<p>\u5f53\u901a\u8fc7win\u4e0b\u7684\u7ec8\u7aef\u5373cmd.exe\u53bb\u6267\u884chello.py\u6587\u4ef6\u65f6\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u3002<\/p>\n<p>python2\u89e3\u91ca\u5668(\u9ed8\u8ba4ASCII\u7f16\u7801)\u53bb\u6309\u58f0\u660e\u7684utf8\u7f16\u7801\u6587\u4ef6\uff0c\u800c\u6587\u4ef6\u53c8\u662futf8\u4fdd\u5b58\u7684\uff0c\u6240\u4ee5\u6ca1\u95ee\u9898\uff1b\u95ee\u9898\u51fa\u5728\u5f53\u6211\u4eecprint\u2019\u82d1\u660a\u2019\u65f6\uff0c\u89e3\u91ca\u5668\u8fd9\u8fb9\u6b63\u5e38\u6267\u884c\uff0c\u4e5f\u4e0d\u4f1a\u62a5\u9519\uff0c\u53ea\u662fprint\u7684\u5185\u5bb9\u4f1a\u4f20\u9012\u7ed9cmd.exe\u7528\u6765\u663e\u793a\uff0c\u800c\u5728py2\u91cc\u8fd9\u4e2a\u5185\u5bb9\u5c31\u662futf8\u7f16\u7801\u7684\u5b57\u8282\u6570\u636e\uff0c\u53efCMD\u9ed8\u8ba4\u7684\u7f16\u7801\u89e3\u7801\u65b9\u5f0f\u662fGBK\uff0c\u6240\u4ee5cmd.exe\u7528GBK\u7684\u89e3\u7801\u65b9\u5f0f\u53bb\u89e3\u7801utf8\u81ea\u7136\u4f1a\u4e71\u7801\u3002<\/p>\n<p>py3\u6b63\u786e\u7684\u539f\u56e0\u662f\u4f20\u9012\u7ed9cmd\u7684\u662funicode\u6570\u636e\uff0ccmd.exe\u53ef\u4ee5\u8bc6\u522b\u5185\u5bb9\uff0c\u6240\u4ee5\u663e\u793a\u6ca1\u95ee\u9898\u3002<\/p>\n<p>\u660e\u767d\u539f\u7406\u4e86\uff0c\u4fee\u6539\u5c31\u6709\u5f88\u591a\u65b9\u5f0f\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre>\n<span>print(u&#x27;\u82d1\u660a&#x27;)<\/span>\n<\/pre>\n\n\n\n<p>\u6539\u6210\u8fd9\u6837\u540e\uff0ccmd\u4e0b\u75282\u4e5f\u4e0d\u4f1a\u6709\u95ee\u9898\u4e86\u3002<\/p>\n<p>\u90e8\u5206\u8f6c\u8f7d\u81ea<a href=\"https:\/\/www.ruanyifeng.com\/blog\/2007\/10\/ascii_unicode_and_utf-8.html\">\u962e\u4e00\u5cf0<\/a>\uff0c\u90e8\u5206\u8f6c\u8f7d\u81ea<a href=\"https:\/\/www.cnblogs.com\/luotianshuai\/articles\/5735051.html\">\u7f57\u5929\u5e05<\/a>\uff0c\u8fd8\u6709\u4e00\u90e8\u5206\u6765\u81ea<a href=\"https:\/\/www.cnblogs.com\/575dsj\/p\/7112767.html\">Kimol<\/a>\uff0c\u611f\u8c22\u3002<\/p>\n<p>\u00a0<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6709\u65f6\u6211\u5e38\u89c9\u5f97\uff0c <br \/>\u4eba\u6d3b\u7740\u5c31\u50cf\u5728\u6ce5\u5730\u4e0a\u884c\u8d70\uff0c <br \/>\u592a\u8fc7\u4e91\u6de1\u98ce\u8f7b\uff0c <br \/>\u56de\u8fc7\u5934\u5c31\u4f1a\u9057\u61be\u4ec0\u4e48\u90fd\u6ca1\u7559\u4e0b\uff0c <br \/>\u8fde\u4e2a\u811a\u5370\u90fd\u6ca1\u6709\uff0c <br \/>\u4f46\u662f\u5fc3\u91cc\u88c5\u7684\u4e1c\u897f\u592a\u91cd\uff0c <br \/>\u4e00\u4e0d\u5c0f\u5fc3\u5c31\u4f1a\u9677\u8fdb\u53bb\uff0c <br \/>\u96be\u4ee5\u81ea\u62d4\u3002<\/p>\n<p style=\"text-align: right;\">\u300a\u5c71\u6708\u4e0d\u77e5\u5fc3\u5e95\u4e8b\u300b<\/p>\n<p style=\"text-align: right;\">\u2014\u2014\u8f9b\u5937\u575e<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASCII \u7801 \u6211\u4eec\u77e5\u9053\uff0c\u8ba1\u7b97\u673a\u5185\u90e8\uff0c\u6240\u6709\u4fe1\u606f\u6700\u7ec8\u90fd\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u503c\u3002\u6bcf\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff08bit\uff09\u67090\u548c1\u4e24\u79cd\u72b6\u6001\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":812,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,4],"tags":[37,15],"_links":{"self":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/308"}],"collection":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=308"}],"version-history":[{"count":0,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/308\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/media\/812"}],"wp:attachment":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}