{"id":170297,"date":"2024-05-08T17:04:47","date_gmt":"2024-05-08T09:04:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/170297.html"},"modified":"2024-05-08T17:04:52","modified_gmt":"2024-05-08T09:04:52","slug":"javascript-%e7%a8%8b%e5%ba%8f%e6%80%8e%e4%b9%88%e8%bf%9b%e8%a1%8c-ascii-%e7%a0%81%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/170297.html","title":{"rendered":"javascript \u7a0b\u5e8f\u600e\u4e48\u8fdb\u884c ascii \u7801\u8f6c\u6362"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27041024\/f0020bff-40db-40b5-a826-36f20fe0155a.webp\" alt=\"javascript \u7a0b\u5e8f\u600e\u4e48\u8fdb\u884c ascii \u7801\u8f6c\u6362\" \/><\/p>\n<p><p>ASCII\uff08\u7f8e\u56fd\u4fe1\u606f\u4ea4\u6362\u6807\u51c6\u4ee3\u7801\uff09\u662f\u6700\u65e9\u7684\u5b57\u7b26\u7f16\u7801\u6807\u51c6\uff0c\u7528\u4e8e\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u8868\u793a\u3002<strong>JavaScript \u4e2d\u8fdb\u884c ASCII \u7801\u8f6c\u6362<\/strong>\u4e3b\u8981\u6d89\u53ca\u4e24\u79cd\u64cd\u4f5c\uff1a\u5b57\u7b26\u8f6c\u6362\u4e3a ASCII \u7801\u4ee5\u53caASCII \u7801\u8f6c\u6362\u56de\u5b57\u7b26\u3002JavaScript \u63d0\u4f9b\u4e86 <code>String<\/code> \u5bf9\u8c61\u7684 <code>charCodeAt()<\/code> \u65b9\u6cd5\u6765\u83b7\u53d6\u4e00\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u3001\u4ee5\u53ca<code>String<\/code> \u7684\u9759\u6001\u65b9\u6cd5 <code>fromCharCode()<\/code> \u6765\u5c06 ASCII \u7801\u8f6c\u6362\u56de\u76f8\u5e94\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26 TO ASCII \u7801<\/h3>\n<\/p>\n<p><p>\u8981\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a ASCII \u7801\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>charCodeAt()<\/code> \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f1a\u63a5\u53d7\u4e00\u4e2a\u7d22\u5f15\uff08\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u4ece 0 \u5f00\u59cb\uff09\uff0c\u5e76\u8fd4\u56de\u8be5\u4f4d\u7f6e\u4e0a\u5b57\u7b26\u7684 ASCII \u7801\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let char = &#039;A&#039;;<\/p>\n<p>let asciiCode = char.charCodeAt(0);<\/p>\n<p>console.log(asciiCode); \/\/ \u8f93\u51fa: 65<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u64cd\u4f5c\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\u9009\u62e9\u4f60\u5e0c\u671b\u8f6c\u6362\u6210 ASCII \u7801\u7684\u5b57\u7b26\u3002<\/li>\n<li>\u4f7f\u7528\u8be5\u5b57\u7b26\u7684 <code>charCodeAt()<\/code> \u65b9\u6cd5\u5e76\u4f20\u5165\u6b63\u786e\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/li>\n<li>\u65b9\u6cd5\u8fd4\u56de\u7684\u6570\u5b57\u5373\u662f\u5bf9\u5e94\u7684 ASCII \u7801\u503c\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001ASCII \u7801 TO \u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u8981\u5c06 ASCII \u7801\u8f6c\u6362\u4e3a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>fromCharCode()<\/code> \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u5c5e\u4e8e String \u5bf9\u8c61\uff0c\u5e76\u4e14\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a ASCII \u7801\u503c\uff0c\u5e76\u5c06\u5b83\u4eec\u8f6c\u6362\u6210\u5bf9\u5e94\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let asciiCode = 65;<\/p>\n<p>let char = String.fromCharCode(asciiCode);<\/p>\n<p>console.log(char); \/\/ \u8f93\u51fa: &#039;A&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u64cd\u4f5c\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\u9009\u62e9\u4f60\u5e0c\u671b\u8f6c\u6362\u6210\u5b57\u7b26\u7684 ASCII \u7801\u503c\u3002<\/li>\n<li>\u4f7f\u7528 <code>String.fromCharCode()<\/code> \u65b9\u6cd5\u5e76\u4f20\u5165\u8be5 ASCII \u7801\u503c\u3002<\/li>\n<li>\u65b9\u6cd5\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u5373\u662f\u5bf9\u5e94\u7684\u5b57\u7b26\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5b57\u7b26\u4e32\u4e0e ASCII \u7801\u4e4b\u95f4\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u901a\u5e38\u4f60\u53ef\u80fd\u9700\u8981\u8f6c\u6362\u6574\u4e2a\u5b57\u7b26\u4e32\u800c\u4e0d\u53ea\u662f\u5355\u4e2a\u5b57\u7b26\u3002\u8fd9\u65f6\uff0c\u4f60\u53ef\u4ee5\u7ed3\u5408\u5faa\u73af\u548c\u4e4b\u524d\u8ba8\u8bba\u8fc7\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u4e0e ASCII \u7801\u4e4b\u95f4\u7684\u5b8c\u6574\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>\u5b57\u7b26\u4e32 TO ASCII \u7801\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let str = &#039;Hello, World!&#039;;<\/p>\n<p>let asciiCodes = [];<\/p>\n<p>for (let i = 0; i &lt; str.length; i++) {<\/p>\n<p>    asciiCodes.push(str.charCodeAt(i));<\/p>\n<p>}<\/p>\n<p>console.log(asciiCodes); \/\/ \u8f93\u51fa: [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>ASCII \u7801\u6570\u7ec4 TO \u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let asciiCodes = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33];<\/p>\n<p>let str = &#039;&#039;;<\/p>\n<p>for (let code of asciiCodes) {<\/p>\n<p>    str += String.fromCharCode(code);<\/p>\n<p>}<\/p>\n<p>console.log(str); \/\/ \u8f93\u51fa: &#039;Hello, World!&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9ad8\u7ea7\u5e94\u7528\u548c\u6ce8\u610f\u70b9<\/h3>\n<\/p>\n<p><p>\u867d\u7136 <code>charCodeAt()<\/code> \u548c <code>fromCharCode()<\/code> \u975e\u5e38\u6613\u7528\uff0c\u5b83\u4eec\u4e3b\u8981\u9488\u5bf9\u4e8e ASCII \u5b57\u7b26\u96c6\u3002\u5982\u679c\u4f60\u9700\u8981\u5904\u7406 Unicode \u5b57\u7b26\u96c6\u4e2d\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\u7684\u5b57\u7b26\uff08\u6bd4\u5982\u8868\u60c5\u7b26\u53f7\u6216\u8005\u5176\u4ed6\u4e00\u4e9b\u8bed\u8a00\u7684\u7279\u6b8a\u5b57\u7b26\uff09\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528 <code>codePointAt()<\/code> \u548c <code>fromCodePoint()<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>Unicode \u5b57\u7b26 TO \u7801\u70b9<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let char = &#039;\ud842\udfb7&#039;;<\/p>\n<p>let codePoint = char.codePointAt(0);<\/p>\n<p>console.log(codePoint); \/\/ \u8f93\u51fa: 134071<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7801\u70b9 TO Unicode \u5b57\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let codePoint = 134071;<\/p>\n<p>let char = String.fromCodePoint(codePoint);<\/p>\n<p>console.log(char); \/\/ \u8f93\u51fa: &#039;\ud842\udfb7&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ol>\n<li>\u5f53\u5904\u7406 Unicode \u5b57\u7b26\u65f6\u9700\u6ce8\u610f <code>charCodeAt()<\/code> \u548c <code>fromCharCode()<\/code> \u53ea\u80fd\u6b63\u786e\u5904\u7406 <code>\\u0000<\/code> \u81f3 <code>\\uFFFF<\/code> \u4e4b\u95f4\u7684\u5b57\u7b26\uff0c\u5bf9\u4e8e\u66f4\u9ad8\u4f4d\u7684\u5b57\u7b26\u5219\u9700\u8981\u4f7f\u7528 <code>codePointAt()<\/code> \u548c <code>fromCodePoint()<\/code> \u65b9\u6cd5\u3002<\/li>\n<li>JavaScript \u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff08immutable\uff09\uff0c\u8fd9\u610f\u5473\u7740\u5b57\u7b26\u4e32\u4e00\u65e6\u521b\u5efa\uff0c\u5b83\u7684\u5185\u5bb9\u65e0\u6cd5\u66f4\u6539\u3002<strong>\u8f6c\u6362 ASCII \u7801\u548c\u5b57\u7b26\u65f6\uff0c\u5e76\u4e0d\u4f1a\u6539\u53d8\u539f\u6709\u5b57\u7b26\u4e32<\/strong>\uff0c\u800c\u662f\u4f1a\u521b\u5efa\u65b0\u7684\u5b57\u7b26\u4e32\u6216\u8005\u6570\u5b57\uff08\u5bf9\u5e94ASCII \u7801\uff09\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u6b63\u786e\u4f7f\u7528 JavaScript \u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8fdb\u884c ASCII \u7801\u548c\u5b57\u7b26\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362\uff0c\u8fd9\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u6587\u672c\u89e3\u6790\u548c\u7f51\u7edc\u901a\u4fe1\u7b49\u9886\u57df\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a ASCII \u7801\uff1f<\/strong><\/p>\n<p>ASCII \u7801\u662f\u7528\u6765\u8868\u793a\u5b57\u7b26\u7684\u5e38\u89c1\u7f16\u7801\u7cfb\u7edf\uff0c\u5728 JavaScript \u4e2d\u8fdb\u884c\u5b57\u7b26\u8f6c\u6362\u4e3a ASCII \u7801\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <code>charCodeAt()<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u6307\u5b9a\u5b57\u7b26\u7684 ASCII \u7801\u503c\u3002\u4f8b\u5982\uff0c<code>&quot;A&quot;.charCodeAt()<\/code> \u5c06\u8fd4\u56de\u5b57\u7b26 &quot;A&quot; \u7684 ASCII \u7801\u503c 65\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u5c06 ASCII \u7801\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5b57\u7b26\uff1f<\/strong><\/p>\n<p>\u8981\u5728 JavaScript \u4e2d\u5c06 ASCII \u7801\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>String.fromCharCode()<\/code> \u51fd\u6570\u3002\u901a\u8fc7\u4f20\u9012 ASCII \u7801\u503c\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u8fd4\u56de\u5bf9\u5e94\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c<code>String.fromCharCode(65)<\/code> \u5c06\u8fd4\u56de\u5b57\u7b26 &quot;A&quot;\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u8fdb\u884c ASCII \u7801\u8f6c\u6362\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528 <code>charCodeAt()<\/code> \u548c <code>String.fromCharCode()<\/code> \u51fd\u6570\u5916\uff0cJavaScript \u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u8fdb\u884c ASCII \u7801\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>parseInt()<\/code> \u51fd\u6570\u5c06 ASCII \u7801\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\uff0c\u6216\u8005\u4f7f\u7528 <code>Number()<\/code> \u51fd\u6570\u5c06 ASCII \u7801\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u540c\u6837\u5730\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>toString()<\/code> \u51fd\u6570\u5c06\u5341\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684 ASCII \u7801\u5b57\u7b26\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u63d0\u4f9b\u4e86\u591a\u79cd\u9009\u9879\uff0c\u4f9b\u4f60\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u9700\u6c42\u7684\u65b9\u5f0f\u8fdb\u884c ASCII \u7801\u8f6c\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"ASCII\uff08\u7f8e\u56fd\u4fe1\u606f\u4ea4\u6362\u6807\u51c6\u4ee3\u7801\uff09\u662f\u6700\u65e9\u7684\u5b57\u7b26\u7f16\u7801\u6807\u51c6\uff0c\u7528\u4e8e\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u8868\u793a\u3002JavaScript \u4e2d\u8fdb\u884c [&hellip;]","protected":false},"author":3,"featured_media":170300,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170297"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=170297"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170297\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/170300"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=170297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=170297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=170297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}