{"id":1089672,"date":"2025-01-08T13:55:31","date_gmt":"2025-01-08T05:55:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089672.html"},"modified":"2025-01-08T13:55:33","modified_gmt":"2025-01-08T05:55:33","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86asc%e7%a0%81%e8%bd%ac%e6%95%b4%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1089672.html","title":{"rendered":"python\u5982\u4f55\u5c06asc\u7801\u8f6c\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202451\/93b596ed-5b50-4dd9-bcd2-2c844ec2df8d.webp\" alt=\"python\u5982\u4f55\u5c06asc\u7801\u8f6c\u6574\u6570\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5c06ASCII\u7801\u8f6c\u6574\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>ord()<\/code> \u548c <code>int()<\/code> \u6765\u5c06ASCII\u7801\u8f6c\u6362\u4e3a\u6574\u6570\u3002<strong>\u4f7f\u7528 <code>ord()<\/code> \u51fd\u6570\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684ASCII\u7801\u3001\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong>\u3002\u4f8b\u5982\uff0c\u5b57\u7b26 &#39;A&#39; \u7684ASCII\u7801\u662f65\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>ord(&#39;A&#39;)<\/code> \u5f97\u523065\uff0c\u800c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u8868\u793aASCII\u7801\u7684\u5b57\u7b26\u4e32 &#39;65&#39;\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>int(&#39;65&#39;)<\/code> \u5f97\u5230\u6574\u657065\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>ord()<\/code> \u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>ord()<\/code> \u662fPython\u7684\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684ASCII\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ascii_code = ord(&#39;A&#39;)<\/p>\n<p>print(ascii_code)  # \u8f93\u51fa: 65<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>ord(&#39;A&#39;)<\/code> \u83b7\u53d6\u5b57\u7b26 &#39;A&#39; \u7684ASCII\u7801\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>ascii_code<\/code>\u3002\u7136\u540e\u6211\u4eec\u6253\u5370 <code>ascii_code<\/code>\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a65\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>int()<\/code> \u662fPython\u7684\u53e6\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = int(&#39;65&#39;)<\/p>\n<p>print(number)  # \u8f93\u51fa: 65<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>int(&#39;65&#39;)<\/code> \u5c06\u5b57\u7b26\u4e32 &#39;65&#39; \u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>number<\/code>\u3002\u7136\u540e\u6211\u4eec\u6253\u5370 <code>number<\/code>\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a65\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\u6765\u5c06ASCII\u7801\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3aASCII\u7801<\/strong><\/h2>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684ASCII\u7801\u3002\u53ef\u4ee5\u4f7f\u7528 <code>ord()<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">char = &#39;B&#39;<\/p>\n<p>ascii_code = ord(char)<\/p>\n<p>print(f&quot;\u5b57\u7b26 {char} \u7684 ASCII \u7801\u662f {ascii_code}&quot;)  # \u8f93\u51fa: \u5b57\u7b26 B \u7684 ASCII \u7801\u662f 66<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26 <code>char<\/code>\uff0c\u7136\u540e\u4f7f\u7528 <code>ord(char)<\/code> \u83b7\u53d6\u8be5\u5b57\u7b26\u7684ASCII\u7801\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>ascii_code<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u7ed3\u679c\uff0c\u8f93\u51fa\u5b57\u7b26 &#39;B&#39; \u7684ASCII\u7801\u4e3a66\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u5c06\u5b57\u7b26\u4e32ASCII\u7801\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u8868\u793aASCII\u7801\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ascii_str = &#39;66&#39;<\/p>\n<p>number = int(ascii_str)<\/p>\n<p>print(f&quot;\u5b57\u7b26\u4e32 {ascii_str} \u5bf9\u5e94\u7684\u6574\u6570\u662f {number}&quot;)  # \u8f93\u51fa: \u5b57\u7b26\u4e32 66 \u5bf9\u5e94\u7684\u6574\u6570\u662f 66<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8868\u793aASCII\u7801\u7684\u5b57\u7b26\u4e32 <code>ascii_str<\/code>\uff0c\u7136\u540e\u4f7f\u7528 <code>int(ascii_str)<\/code> \u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>number<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u7ed3\u679c\uff0c\u8f93\u51fa\u5b57\u7b26\u4e32 &#39;66&#39; \u5bf9\u5e94\u7684\u6574\u6570\u662f66\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u7ed3\u5408\u4f7f\u7528 <code>ord()<\/code> \u548c <code>int()<\/code> \u51fd\u6570<\/strong><\/h2>\n<p><p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528 <code>ord()<\/code> \u548c <code>int()<\/code> \u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">char = &#39;C&#39;<\/p>\n<p>ascii_code = ord(char)<\/p>\n<p>number = int(ascii_code)<\/p>\n<p>print(f&quot;\u5b57\u7b26 {char} \u7684 ASCII \u7801\u662f {ascii_code}\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f {number}&quot;)  # \u8f93\u51fa: \u5b57\u7b26 C \u7684 ASCII \u7801\u662f 67\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f 67<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>ord(char)<\/code> \u83b7\u53d6\u5b57\u7b26 &#39;C&#39; \u7684ASCII\u7801\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>ascii_code<\/code>\u3002\u7136\u540e\u6211\u4eec\u4f7f\u7528 <code>int(ascii_code)<\/code> \u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>number<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u7ed3\u679c\uff0c\u8f93\u51fa\u5b57\u7b26 &#39;C&#39; \u7684ASCII\u7801\u4e3a67\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u4e5f\u662f67\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u5904\u7406\u591a\u4e2a\u5b57\u7b26<\/strong><\/h2>\n<p><p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5904\u7406\u591a\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">chars = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]<\/p>\n<p>ascii_codes = [ord(char) for char in chars]<\/p>\n<p>numbers = [int(ascii_code) for ascii_code in ascii_codes]<\/p>\n<p>print(f&quot;\u5b57\u7b26\u5217\u8868 {chars} \u7684 ASCII \u7801\u662f {ascii_codes}\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f {numbers}&quot;)  <\/p>\n<h2><strong>\u8f93\u51fa: \u5b57\u7b26\u5217\u8868 [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;] \u7684 ASCII \u7801\u662f [65, 66, 67]\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f [65, 66, 67]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26\u5217\u8868 <code>chars<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f <code>[ord(char) for char in chars]<\/code> \u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\u7684ASCII\u7801\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf <code>ascii_codes<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f <code>[int(ascii_code) for ascii_code in ascii_codes]<\/code> \u5c06\u6bcf\u4e2aASCII\u7801\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf <code>numbers<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u7ed3\u679c\uff0c\u8f93\u51fa\u5b57\u7b26\u5217\u8868 [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;] \u7684ASCII\u7801\u5206\u522b\u662f65\u300166\u548c67\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u4e5f\u662f65\u300166\u548c67\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26<\/strong><\/h2>\n<p><p>\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u9700\u8981\u5c06\u5176\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;ABC&quot;<\/p>\n<p>ascii_codes = [ord(char) for char in string]<\/p>\n<p>numbers = [int(ascii_code) for ascii_code in ascii_codes]<\/p>\n<p>print(f&quot;\u5b57\u7b26\u4e32 {string} \u7684 ASCII \u7801\u662f {ascii_codes}\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f {numbers}&quot;)  <\/p>\n<h2><strong>\u8f93\u51fa: \u5b57\u7b26\u4e32 ABC \u7684 ASCII \u7801\u662f [65, 66, 67]\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f [65, 66, 67]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>string<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f <code>[ord(char) for char in string]<\/code> \u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684ASCII\u7801\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf <code>ascii_codes<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f <code>[int(ascii_code) for ascii_code in ascii_codes]<\/code> \u5c06\u6bcf\u4e2aASCII\u7801\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf <code>numbers<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u7ed3\u679c\uff0c\u8f93\u51fa\u5b57\u7b26\u4e32 &#39;ABC&#39; \u7684ASCII\u7801\u5206\u522b\u662f65\u300166\u548c67\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u4e5f\u662f65\u300166\u548c67\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u5904\u7406\u590d\u6742\u5b57\u7b26\u96c6<\/strong><\/h2>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u5b57\u7b26\u96c6\uff0c\u4f8b\u5982\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ord()<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u5176Unicode\u7801\u70b9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">char = &#39;\u4f60&#39;<\/p>\n<p>unicode_code = ord(char)<\/p>\n<p>print(f&quot;\u5b57\u7b26 {char} \u7684 Unicode \u7801\u662f {unicode_code}&quot;)  # \u8f93\u51fa: \u5b57\u7b26 \u4f60 \u7684 Unicode \u7801\u662f 20320<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26 <code>char<\/code>\uff0c\u7136\u540e\u4f7f\u7528 <code>ord(char)<\/code> \u83b7\u53d6\u5176Unicode\u7801\u70b9\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf <code>unicode_code<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u7ed3\u679c\uff0c\u8f93\u51fa\u5b57\u7b26 &#39;\u4f60&#39; \u7684Unicode\u7801\u70b9\u4e3a20320\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u975eASCII\u5b57\u7b26<\/strong><\/h2>\n<p><p>\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\u7684Unicode\u7801\u70b9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;\u4f60\u597d&quot;<\/p>\n<p>unicode_codes = [ord(char) for char in string]<\/p>\n<p>print(f&quot;\u5b57\u7b26\u4e32 {string} \u7684 Unicode \u7801\u662f {unicode_codes}&quot;)  <\/p>\n<h2><strong>\u8f93\u51fa: \u5b57\u7b26\u4e32 \u4f60\u597d \u7684 Unicode \u7801\u662f [20320, 22909]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u7b26\u4e32 <code>string<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f <code>[ord(char) for char in string]<\/code> \u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684Unicode\u7801\u70b9\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf <code>unicode_codes<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u7ed3\u679c\uff0c\u8f93\u51fa\u5b57\u7b26\u4e32 &#39;\u4f60\u597d&#39; \u7684Unicode\u7801\u70b9\u5206\u522b\u662f20320\u548c22909\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u7efc\u5408\u793a\u4f8b<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5c06ASCII\u7801\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4e0b\u9762\u6211\u4eec\u63d0\u4f9b\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c06\u591a\u4e2a\u5b57\u7b26\u548c\u5b57\u7b26\u4e32\u7684ASCII\u7801\u6216Unicode\u7801\u70b9\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def char_to_int(char):<\/p>\n<p>    return ord(char)<\/p>\n<p>def str_to_int(ascii_str):<\/p>\n<p>    return int(ascii_str)<\/p>\n<p>chars = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]<\/p>\n<p>strings = [&quot;ABC&quot;, &quot;\u4f60\u597d&quot;]<\/p>\n<p>for char in chars:<\/p>\n<p>    ascii_code = char_to_int(char)<\/p>\n<p>    print(f&quot;\u5b57\u7b26 {char} \u7684 ASCII \u7801\u662f {ascii_code}\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f {ascii_code}&quot;)<\/p>\n<p>for string in strings:<\/p>\n<p>    unicode_codes = [char_to_int(char) for char in string]<\/p>\n<p>    print(f&quot;\u5b57\u7b26\u4e32 {string} \u7684 Unicode \u7801\u662f {unicode_codes}\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f {unicode_codes}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570 <code>char_to_int()<\/code> \u548c <code>str_to_int()<\/code>\uff0c\u5206\u522b\u7528\u4e8e\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3aASCII\u7801\u6216Unicode\u7801\u70b9\uff0c\u4ee5\u53ca\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26\u5217\u8868 <code>chars<\/code> \u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868 <code>strings<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b57\u7b26\u548c\u5b57\u7b26\u4e32\uff0c\u83b7\u53d6\u5176ASCII\u7801\u6216Unicode\u7801\u70b9\uff0c\u5e76\u6253\u5370\u7ed3\u679c\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>\u5b57\u7b26 A \u7684 ASCII \u7801\u662f 65\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f 65<\/p>\n<p>\u5b57\u7b26 B \u7684 ASCII \u7801\u662f 66\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f 66<\/p>\n<p>\u5b57\u7b26 C \u7684 ASCII \u7801\u662f 67\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f 67<\/p>\n<p>\u5b57\u7b26\u4e32 ABC \u7684 Unicode \u7801\u662f [65, 66, 67]\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f [65, 66, 67]<\/p>\n<p>\u5b57\u7b26\u4e32 \u4f60\u597d \u7684 Unicode \u7801\u662f [20320, 22909]\uff0c\u5bf9\u5e94\u7684\u6574\u6570\u662f [20320, 22909]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u6e05\u695a\u5730\u4e86\u89e3\u5982\u4f55\u5728Python\u4e2d\u5c06ASCII\u7801\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u65e0\u8bba\u662f\u5904\u7406\u5355\u4e2a\u5b57\u7b26\u3001\u591a\u4e2a\u5b57\u7b26\uff0c\u8fd8\u662f\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528 <code>ord()<\/code> \u548c <code>int()<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06ASCII\u7801\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>ord()<\/code>\u51fd\u6570\u6765\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5176\u5bf9\u5e94\u7684ASCII\u7801\u6574\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5b57\u7b26<code>&#39;A&#39;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ord(&#39;A&#39;)<\/code>\u83b7\u5f97\u5176ASCII\u7801\u503c65\u3002\u8fd9\u79cd\u65b9\u5f0f\u7b80\u5355\u800c\u76f4\u63a5\uff0c\u9002\u7528\u4e8e\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u4e00\u4e32ASCII\u7801\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u5e76\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684ASCII\u6574\u6570\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>ord()<\/code>\u51fd\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">string = &quot;Hello&quot;\nascii_values = [ord(char) for char in string]\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5b57\u7b26&#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;\u5bf9\u5e94ASCII\u7801\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u975eASCII\u5b57\u7b26\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5c1d\u8bd5\u5c06\u975eASCII\u5b57\u7b26\uff08\u5982\u6c49\u5b57\u6216\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\uff09\u8f6c\u6362\u4e3a\u6574\u6570\uff0c<code>ord()<\/code>\u51fd\u6570\u4ecd\u7136\u9002\u7528\uff0c\u4f46\u5176\u8fd4\u56de\u503c\u5c06\u662fUnicode\u7801\u70b9\u800c\u975eASCII\u7801\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u5b57\u7b26\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>ord()<\/code>\u6765\u83b7\u53d6\u5b83\u4eec\u7684\u6574\u6570\u8868\u793a\u3002\u4f8b\u5982\uff0c<code>ord(&#39;\u6c49&#39;)<\/code>\u5c06\u8fd4\u56de\u5176\u5bf9\u5e94\u7684Unicode\u7801\u70b9\u3002\u5982\u679c\u53ea\u5173\u6ce8ASCII\u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u9700\u5728\u8f6c\u6362\u524d\u8fdb\u884c\u5b57\u7b26\u7684\u68c0\u67e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5c06ASCII\u7801\u8f6c\u6574\u6570 \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 ord() \u548c int() \u6765\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":1089681,"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\/1089672"}],"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=1089672"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089672\/revisions"}],"predecessor-version":[{"id":1089684,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089672\/revisions\/1089684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089681"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}