{"id":3307,"date":"2024-11-10T17:27:35","date_gmt":"2024-11-10T09:27:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3307\/"},"modified":"2024-11-10T17:27:35","modified_gmt":"2024-11-10T09:27:35","slug":"js%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3307\/","title":{"rendered":"js\u5982\u4f55\u5b9a\u4e49\u7f16\u7801"},"content":{"rendered":"<blockquote><p>\n  javascript \u4e2d\u7684\u7f16\u7801\u662f\u6307\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\u6216\u5c06\u5b57\u8282\u5e8f\u5217\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u3002\u5b83\u63d0\u4f9b\u4e86 utf-8\u3001utf-16\u3001base64 \u548c ascii \u7b49\u5185\u5efa\u7f16\u7801\u7c7b\u578b\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 textencoder \u548c textdecoder api \u5b9a\u4e49\u7f16\u7801\uff0c\u4f8b\u5982\u4f7f\u7528 encoder.encode() \u7f16\u7801\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 decoder.decode() \u89e3\u7801\u5b57\u8282\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110809273065056.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5b9a\u4e49\u7f16\u7801\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5b9a\u4e49\u7f16\u7801\u63d2\u56fe\" \/><\/p>\n<p><strong>JavaScript \u4e2d\u7684\u7f16\u7801\u5b9a\u4e49<\/strong><\/p>\n<p><strong>\u5982\u4f55\u5b9a\u4e49\u7f16\u7801\uff1f<\/strong><\/p>\n<p>JavaScript \u4e2d\u7684\u7f16\u7801\u662f\u6307\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\u6216\u5c06\u5b57\u8282\u5e8f\u5217\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u3002\u5b83\u5141\u8bb8\u60a8\u5728\u4e0d\u540c\u5b57\u7b26\u96c6\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u4ece\u800c\u786e\u4fdd\u6570\u636e\u80fd\u591f\u5728\u5404\u79cd\u7cfb\u7edf\u548c\u8bbe\u5907\u4e4b\u95f4\u6b63\u786e\u4f20\u8f93\u548c\u5b58\u50a8\u3002<\/p>\n<p><strong>\u7f16\u7801\u7c7b\u578b<\/strong><\/p>\n<p>JavaScript \u63d0\u4f9b\u4e86\u51e0\u79cd\u5185\u5efa\u7684\u7f16\u7801\u7c7b\u578b\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>UTF-8\uff1a<\/strong>\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684 Unicode \u7f16\u7801\uff0c\u7528\u4e8e\u8868\u793a\u6700\u5e38\u7528\u7684\u8bed\u8a00\u3002<\/li>\n<li> <strong>UTF-16\uff1a<\/strong>\u53e6\u4e00\u79cd Unicode \u7f16\u7801\uff0c\u901a\u5e38\u7528\u4e8e\u8868\u793a\u975e\u62c9\u4e01\u5b57\u7b26\u3002<\/li>\n<li> <strong>Base64\uff1a<\/strong>\u4e00\u79cd\u4e8c\u8fdb\u5236\u5230\u6587\u672c\u7f16\u7801\uff0c\u7528\u4e8e\u6570\u636e\u3002<\/li>\n<li> <strong>ASCII\uff1a<\/strong>\u4e00\u79cd\u7b80\u5355\u7684\u7f16\u7801\uff0c\u7528\u4e8e\u8868\u793a\u82f1\u8bed\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u7b26\u53f7\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9a\u4e49\u7f16\u7801<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 TextEncoder \u548c TextDecoder API \u6765\u5b9a\u4e49\u7f16\u7801\uff1a<\/p>\n<pre>const encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\nconst encodedString = encoder.encode(\"Hello, JavaScript!\");\nconst decodedString = decoder.decode(encodedString);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 TextEncoder \u7f16\u7801\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>const encoder = new TextEncoder(\"utf-8\");\n\nconst encodedString = encoder.encode(\"\u4f60\u597d\uff0c\u4e16\u754c\");\n\nconsole.log(encodedString); \/\/ \u8f93\u51fa\uff1aArrayBuffer { byteLength: 12 }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 TextDecoder \u89e3\u7801\u5b57\u8282\u6570\u7ec4\uff1a<\/p>\n<pre>const decoder = new TextDecoder(\"utf-8\");\n\nconst encodedString = new Uint8Array([228, 189, 160, 229, 165, 189, 32, 229, 190, 128, 32, 214, 162, 130, 172, 141]);\n\nconst decodedString = decoder.decode(encodedString);\n\nconsole.log(decodedString); \/\/ \u8f93\u51fa\uff1a\u4f60\u597d\uff0c\u4e16\u754c<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5b9a\u4e49\u7f16\u7801\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u4e2d\u7684\u7f16\u7801\u662f\u6307\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\u6216\u5c06\u5b57\u8282\u5e8f\u5217\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u3002\u5b83\u63d0\u4f9b\u4e86 utf-8\u3001utf-16\u3001base64 \u548c ascii \u7b49\u5185\u5efa\u7f16\u7801\u7c7b\u578b\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 textencoder \u548c textdecoder api \u5b9a\u4e49\u7f16\u7801\uff0c\u4f8b\u5982\u4f7f\u7528 encoder.encode() \u7f16\u7801\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 decoder.decode() \u89e3\u7801\u5b57\u8282\u6570\u7ec4\u3002 JavaScript \u4e2d\u7684\u7f16\u7801\u5b9a\u4e49 \u5982\u4f55\u5b9a\u4e49\u7f16\u7801\uff1f JavaScript \u4e2d\u7684\u7f16\u7801\u662f\u6307\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\u6216\u5c06\u5b57\u8282\u5e8f\u5217\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u3002\u5b83\u5141\u8bb8\u60a8\u5728\u4e0d\u540c\u5b57\u7b26\u96c6\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u4ece\u800c\u786e\u4fdd\u6570\u636e\u80fd\u591f\u5728\u5404\u79cd\u7cfb\u7edf\u548c\u8bbe\u5907\u4e4b\u95f4\u6b63\u786e\u4f20\u8f93\u548c\u5b58\u50a8\u3002 \u7f16\u7801\u7c7b\u578b JavaScript \u63d0\u4f9b\u4e86\u51e0\u79cd\u5185\u5efa\u7684\u7f16\u7801\u7c7b\u578b\uff0c\u5305\u62ec\uff1a UTF-8\uff1a\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684 Unicode \u7f16\u7801\uff0c\u7528\u4e8e\u8868\u793a\u6700\u5e38\u7528\u7684\u8bed\u8a00\u3002 UTF-16\uff1a\u53e6\u4e00\u79cd Unicode \u7f16\u7801\uff0c\u901a\u5e38\u7528\u4e8e\u8868\u793a\u975e\u62c9\u4e01\u5b57\u7b26\u3002 Base64\uff1a\u4e00\u79cd\u4e8c\u8fdb\u5236\u5230\u6587\u672c\u7f16\u7801\uff0c\u7528\u4e8e\u6570\u636e\u3002 ASCII\uff1a\u4e00\u79cd\u7b80\u5355\u7684\u7f16\u7801\uff0c\u7528\u4e8e\u8868\u793a\u82f1\u8bed\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u7b26\u53f7\u3002 \u5b9a\u4e49\u7f16\u7801 \u60a8\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 TextEncoder \u548c TextDecoder API \u6765\u5b9a\u4e49\u7f16\u7801\uff1a const encoder = new TextEncoder(); const decoder = new TextDecoder(); const encodedString = encoder.encode(&#8220;Hello, JavaScript!&#8221;); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3307","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3307"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3307\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}