{"id":1088180,"date":"2025-01-08T13:42:02","date_gmt":"2025-01-08T05:42:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1088180.html"},"modified":"2025-01-08T13:42:04","modified_gmt":"2025-01-08T05:42:04","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e8%bd%ac%e5%8c%96%e4%b8%baascii-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1088180.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5b57\u7b26\u8f6c\u5316\u4e3aascii"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200747\/139d0876-4c91-46d3-99a2-dbef8dc25be6.webp\" alt=\"python\u5982\u4f55\u5c06\u5b57\u7b26\u8f6c\u5316\u4e3aascii\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5c06\u5b57\u7b26\u8f6c\u5316\u4e3aASCII\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>ord()<\/code>\u51fd\u6570\u3002<strong>\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u5e76\u8fd4\u56de\u5176\u5bf9\u5e94\u7684ASCII\u503c<\/strong>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u77e5\u9053\u5b57\u7b26&#39;A&#39;\u7684ASCII\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ord(&#39;A&#39;)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de65\u3002<strong>\u8be5\u65b9\u6cd5\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5355\u4e2a\u5b57\u7b26\u7684\u8f6c\u6362<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5b83\uff0c\u5e76\u6db5\u76d6\u66f4\u590d\u6742\u7684\u60c5\u5883\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4ec0\u4e48\u662fASCII\u7801<\/h3>\n<\/p>\n<p><p><strong>ASCII<\/strong>\uff08American Standard Code for Information Interchange\uff0c\u7f8e\u56fd\u4fe1\u606f\u4ea4\u6362\u6807\u51c6\u4ee3\u7801\uff09\u662f\u5b57\u7b26\u7f16\u7801\u7684\u4e00\u79cd\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u663e\u793a\u73b0\u4ee3\u82f1\u8bed\u5b57\u7b26\u548c\u63a7\u5236\u5b57\u7b26\u7684\u6807\u51c6\u3002\u6bcf\u4e2a\u5b57\u7b26\u5bf9\u5e94\u4e00\u4e2a\u552f\u4e00\u7684\u6570\u503c\uff0c\u8fd9\u4e9b\u6570\u503c\u57280\u5230127\u4e4b\u95f4\u3002\u4f8b\u5982\uff0c\u5b57\u6bcd&#39;A&#39;\u7684ASCII\u503c\u662f65\uff0c\u800c\u5b57\u6bcd&#39;a&#39;\u7684ASCII\u503c\u662f97\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u8f6c\u5316\u4e3aASCII<\/h3>\n<\/p>\n<p><p><strong><code>ord()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5feb\u901f\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5176\u5bf9\u5e94\u7684ASCII\u503c<\/strong>\u3002\u5b83\u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u8fd4\u56de\u5176\u5bf9\u5e94\u7684\u6574\u6570\u503c\u3002\u5982\u679c\u8f93\u5165\u7684\u4e0d\u662f\u5355\u4e2a\u5b57\u7b26\uff0c\u4f1a\u5f15\u53d1TypeError\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">char = &#39;A&#39;<\/p>\n<p>ascii_value = ord(char)<\/p>\n<p>print(f&quot;The ASCII value of &#39;{char}&#39; is {ascii_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>ord(&#39;A&#39;)<\/code>\u8fd4\u56de65\uff0c\u8868\u793a\u5b57\u7b26&#39;A&#39;\u7684ASCII\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6279\u91cf\u8f6c\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u4e3aASCII<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u90fd\u8f6c\u6362\u4e3a\u5176ASCII\u503c\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def string_to_ascii(string):<\/p>\n<p>    ascii_values = [ord(char) for char in string]<\/p>\n<p>    return ascii_values<\/p>\n<p>string = &quot;Hello&quot;<\/p>\n<p>ascii_values = string_to_ascii(string)<\/p>\n<p>print(f&quot;The ASCII values of the string &#39;{string}&#39; are {ascii_values}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>string_to_ascii<\/code>\u51fd\u6570\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3aASCII\u503c\u3002\u6700\u540e\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709ASCII\u503c\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u7279\u6b8a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p><strong>\u7279\u6b8a\u5b57\u7b26<\/strong>\u548c<strong>\u63a7\u5236\u5b57\u7b26<\/strong>\u4e5f\u6709\u5176\u5bf9\u5e94\u7684ASCII\u503c\u3002\u4f8b\u5982\uff0c\u6362\u884c\u7b26<code>\\n<\/code>\u7684ASCII\u503c\u662f10\uff0c\u5236\u8868\u7b26<code>\\t<\/code>\u7684ASCII\u503c\u662f9\u3002\u4f60\u53ef\u4ee5\u50cf\u5904\u7406\u666e\u901a\u5b57\u7b26\u4e00\u6837\uff0c\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">special_char = &#39;\\n&#39;<\/p>\n<p>ascii_value = ord(special_char)<\/p>\n<p>print(f&quot;The ASCII value of the special character &#39;\\\\n&#39; is {ascii_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5f53\u8f93\u5165\u7684\u5b57\u7b26\u4e0d\u662f\u5355\u4e2a\u5b57\u7b26\u65f6\uff0c<code>ord()<\/code>\u51fd\u6570\u4f1a\u5f15\u53d1TypeError\u3002\u4e3a\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5e94\u8be5\u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_ord(char):<\/p>\n<p>    try:<\/p>\n<p>        return ord(char)<\/p>\n<p>    except TypeError:<\/p>\n<p>        print(f&quot;Error: &#39;{char}&#39; is not a single character.&quot;)<\/p>\n<p>        return None<\/p>\n<p>char = &quot;AB&quot;<\/p>\n<p>ascii_value = safe_ord(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>safe_ord<\/code>\u51fd\u6570\u5728\u8c03\u7528<code>ord()<\/code>\u4e4b\u524d\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff0c\u786e\u4fdd\u8f93\u5165\u7684\u5b57\u7b26\u662f\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u7b26\u7f16\u7801\u7684\u5176\u4ed6\u65b9\u5f0f<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>ord()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f46\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3aASCII\u7801\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u7684<code>pack<\/code>\u548c<code>unpack<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>char = &#39;A&#39;<\/p>\n<p>ascii_value = struct.unpack(&#39;B&#39;, char.encode())[0]<\/p>\n<p>print(f&quot;The ASCII value of &#39;{char}&#39; using struct is {ascii_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>struct.unpack<\/code>\u5c06\u5b57\u7b26\u7f16\u7801\u4e3a\u5b57\u8282\uff0c\u7136\u540e\u5c06\u5176\u89e3\u5305\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5c06\u5b57\u7b26\u8f6c\u5316\u4e3aASCII\u503c\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728<strong>\u52a0\u5bc6<\/strong>\u3001<strong>\u6570\u636e\u538b\u7f29<\/strong>\u3001<strong>\u901a\u4fe1\u534f\u8bae<\/strong>\u7b49\u9886\u57df\uff0c\u90fd\u9700\u8981\u5904\u7406\u5b57\u7b26\u548cASCII\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u4e2d\u5c06\u5b57\u7b26\u8f6c\u5316\u4e3aASCII\u503c\u7684\u65b9\u6cd5\u4e3b\u8981\u662f\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570<\/strong>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u3001\u9ad8\u6548\u4e14\u9002\u7528\u4e8e\u5404\u79cd\u5b57\u7b26\uff0c\u5305\u62ec\u666e\u901a\u5b57\u7b26\u548c\u7279\u6b8a\u5b57\u7b26\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u6765\u6279\u91cf\u8f6c\u6362\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u5982<code>struct<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u65e0\u8bba\u662f\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u548c\u638c\u63e1\u5b57\u7b26\u4e0eASCII\u7801\u7684\u8f6c\u6362\u662f\u5904\u7406\u6587\u672c\u6570\u636e\u7684\u91cd\u8981\u6280\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u8f6c\u5316\u4e3aASCII\u503c\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u5e94\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\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\u5728Python\u4e2d\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3aASCII\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>ord()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684ASCII\u7801\u3002\u4f8b\u5982\uff0c<code>ord(&#39;A&#39;)<\/code>\u4f1a\u8fd4\u56de65\uff0c\u8fd9\u662f\u5b57\u6bcdA\u7684ASCII\u503c\u3002\u5bf9\u4e8e\u4efb\u4f55\u5355\u4e2a\u5b57\u7b26\uff0c\u6b64\u65b9\u6cd5\u90fd\u6709\u6548\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3aASCII\u7801\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5176\u5bf9\u5e94\u7684ASCII\u7801\u3002\u4f8b\u5982\uff1a<code>ascii_values = [ord(char) for char in &#39;Hello&#39;]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542bH\u3001e\u3001l\u3001l\u548co\u5404\u81eaASCII\u503c\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06ASCII\u7801\u8f6c\u6362\u56de\u5b57\u7b26\uff1f<\/strong><br \/>\u8981\u5c06ASCII\u7801\u8f6c\u6362\u56de\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528<code>chr()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2aASCII\u7801\uff0c\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c<code>chr(65)<\/code>\u4f1a\u8fd4\u56de&#39;A&#39;\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4eceASCII\u503c\u6062\u590d\u539f\u59cb\u5b57\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u5b57\u7b26\u8f6c\u5316\u4e3aASCII\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684ord()\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u5e76\u8fd4\u56de\u5176\u5bf9\u5e94\u7684AS [&hellip;]","protected":false},"author":3,"featured_media":1088189,"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\/1088180"}],"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=1088180"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088180\/revisions"}],"predecessor-version":[{"id":1088192,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088180\/revisions\/1088192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1088189"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1088180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1088180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1088180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}