{"id":1162704,"date":"2025-01-15T14:46:37","date_gmt":"2025-01-15T06:46:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1162704.html"},"modified":"2025-01-15T14:46:41","modified_gmt":"2025-01-15T06:46:41","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1162704.html","title":{"rendered":"python\u5982\u4f55\u8f93\u5165\u5341\u516d\u8fdb\u5236"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25203639\/7f4cc160-9613-4bef-b237-eac4dcff6f24.webp\" alt=\"python\u5982\u4f55\u8f93\u5165\u5341\u516d\u8fdb\u5236\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u8f93\u5165\u5341\u516d\u8fdb\u5236<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u8f93\u5165\u5341\u516d\u8fdb\u5236\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>0x<\/code>\u524d\u7f00\u8868\u793a\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>int()<\/code>\u8fdb\u884c\u8f6c\u6362\u3001\u4f7f\u7528<code>hex()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u8fdb\u884c\u8f6c\u6362<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>0x<\/code>\u524d\u7f00\u8868\u793a\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8f93\u5165\u548c\u5904\u7406\u5341\u516d\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>0x<\/code>\u524d\u7f00\u8868\u793a<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5728\u6570\u5b57\u524d\u52a0\u4e0a<code>0x<\/code>\u524d\u7f00\u6765\u8868\u793a\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_num = 0x1A<\/p>\n<p>print(hex_num)  # \u8f93\u51fa 26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>0x1A<\/code>\u8868\u793a\u5341\u516d\u8fdb\u5236\u65701A\uff0c\u5176\u5341\u8fdb\u5236\u503c\u4e3a26\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u6700\u4e3a\u76f4\u63a5\u548c\u5e38\u7528\u3002\u5f53\u4f60\u5728\u4ee3\u7801\u4e2d\u770b\u5230\u4ee5<code>0x<\/code>\u5f00\u5934\u7684\u6570\u5b57\u65f6\uff0c\u5c31\u77e5\u9053\u8fd9\u662f\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u3002Python\u4f1a\u81ea\u52a8\u5c06\u5176\u89e3\u91ca\u4e3a\u5bf9\u5e94\u7684\u5341\u8fdb\u5236\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>int()<\/code>\u8fdb\u884c\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>int()<\/code>\u5c06\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_str = &quot;1A&quot;<\/p>\n<p>dec_num = int(hex_str, 16)<\/p>\n<p>print(dec_num)  # \u8f93\u51fa 26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>int()<\/code>\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u4e86\u6570\u5b57\u7684\u57fa\u6570\uff0c<code>16<\/code>\u8868\u793a\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\u65f6\uff0c\u53ef\u4ee5\u5904\u7406\u4efb\u4f55\u57fa\u6570\u7684\u5b57\u7b26\u4e32\u3002\u53ea\u9700\u6307\u5b9a\u9002\u5f53\u7684\u57fa\u6570\u53c2\u6570\uff0cPython\u5c31\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u5341\u8fdb\u5236\u6570\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5404\u79cd\u4e0d\u540c\u7684\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528<code>hex()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>hex()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5341\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dec_num = 26<\/p>\n<p>hex_str = hex(dec_num)<\/p>\n<p>print(hex_str)  # \u8f93\u51fa 0x1a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>hex()<\/code>\u51fd\u6570\u5c06\u5341\u8fdb\u5236\u657026\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32<code>0x1a<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p><code>hex()<\/code>\u51fd\u6570\u975e\u5e38\u65b9\u4fbf\uff0c\u7528\u4e8e\u5c06\u5341\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u8868\u793a\u3002\u5f53\u9700\u8981\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u8fdb\u884c\u8f93\u51fa\u6216\u8fdb\u4e00\u6b65\u5904\u7406\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u8fdb\u884c\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5341\u516d\u8fdb\u5236\u6570\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dec_num = 26<\/p>\n<p>hex_str = &quot;{:x}&quot;.format(dec_num)<\/p>\n<p>print(hex_str)  # \u8f93\u51fa 1a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>{:x}<\/code>\u8868\u793a\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u5c0f\u5199\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u7528\u4e8e\u591a\u79cd\u683c\u5f0f\u7684\u6570\u503c\u8f6c\u6362\u548c\u8f93\u51fa\u3002\u901a\u8fc7\u6307\u5b9a\u9002\u5f53\u7684\u683c\u5f0f\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u7b49\u4e0d\u540c\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5904\u7406\u8f93\u5165\u7684\u5341\u516d\u8fdb\u5236\u6570<\/h2>\n<\/p>\n<p><h3>\u4ece\u7528\u6237\u8f93\u5165\u4e2d\u83b7\u53d6\u5341\u516d\u8fdb\u5236\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\uff08\u4e0d\u5e260x\u524d\u7f00\uff09\uff1a&quot;)<\/p>\n<p>dec_num = int(hex_input, 16)<\/p>\n<p>print(f&quot;\u5341\u8fdb\u5236\u6570\u503c\u4e3a\uff1a{dec_num}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u5e26\u6709<code>0x<\/code>\u524d\u7f00\u7684\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u5341\u516d\u8fdb\u5236\u6570\u5e26\u6709<code>0x<\/code>\u524d\u7f00\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5207\u7247\u529f\u80fd\u53bb\u6389\u524d\u7f00\uff0c\u518d\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5e260x\u524d\u7f00\u7684\u5341\u516d\u8fdb\u5236\u6570\uff1a&quot;)<\/p>\n<p>if hex_input.startswith(&quot;0x&quot;):<\/p>\n<p>    hex_input = hex_input[2:]<\/p>\n<p>dec_num = int(hex_input, 16)<\/p>\n<p>print(f&quot;\u5341\u8fdb\u5236\u6570\u503c\u4e3a\uff1a{dec_num}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u5341\u516d\u8fdb\u5236\u4e0e\u5176\u4ed6\u8fdb\u5236\u7684\u8f6c\u6362<\/h2>\n<\/p>\n<p><h3>\u5341\u516d\u8fdb\u5236\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>bin()<\/code>\u51fd\u6570\u5c06\u5341\u516d\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_num = 0x1A<\/p>\n<p>bin_str = bin(hex_num)<\/p>\n<p>print(bin_str)  # \u8f93\u51fa 0b11010<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u8fdb\u5236\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>oct()<\/code>\u51fd\u6570\u5c06\u5341\u516d\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_num = 0x1A<\/p>\n<p>oct_str = oct(hex_num)<\/p>\n<p>print(oct_str)  # \u8f93\u51fa 0o32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u5404\u79cd\u8fdb\u5236\u95f4\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def hex_to_bin(hex_str):<\/p>\n<p>    return bin(int(hex_str, 16))<\/p>\n<p>def hex_to_oct(hex_str):<\/p>\n<p>    return oct(int(hex_str, 16))<\/p>\n<p>print(hex_to_bin(&quot;1A&quot;))  # \u8f93\u51fa 0b11010<\/p>\n<p>print(hex_to_oct(&quot;1A&quot;))  # \u8f93\u51fa 0o32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u5904\u7406\u5927\u6570\u548c\u8d1f\u6570<\/h2>\n<\/p>\n<p><h3>\u5904\u7406\u5927\u6570<\/h3>\n<\/p>\n<p><p>Python\u53ef\u4ee5\u5904\u7406\u975e\u5e38\u5927\u7684\u5341\u516d\u8fdb\u5236\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_num = 0x123456789ABCDEF<\/p>\n<p>print(hex_num)  # \u8f93\u51fa 81985529216486895<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u8d1f\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u8d1f\u53f7\u548c<code>0x<\/code>\u524d\u7f00\u8868\u793a\u8d1f\u7684\u5341\u516d\u8fdb\u5236\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_num = -0x1A<\/p>\n<p>print(hex_num)  # \u8f93\u51fa -26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u5b9e\u6218\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>\u5728\u6587\u4ef6\u4e2d\u5b58\u50a8\u548c\u8bfb\u53d6\u5341\u516d\u8fdb\u5236\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u5341\u516d\u8fdb\u5236\u6570\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8bfb\u53d6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u5341\u516d\u8fdb\u5236\u6570\u5230\u6587\u4ef6<\/p>\n<p>with open(&quot;hex_data.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;0x1A&quot;)<\/p>\n<h2><strong>\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5341\u516d\u8fdb\u5236\u6570<\/strong><\/h2>\n<p>with open(&quot;hex_data.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    hex_str = file.read().strip()<\/p>\n<p>    dec_num = int(hex_str, 16)<\/p>\n<p>    print(dec_num)  # \u8f93\u51fa 26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u5341\u516d\u8fdb\u5236\u6570\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u5341\u516d\u8fdb\u5236\u6570\u7ecf\u5e38\u7528\u4e8e\u8868\u793a\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import binascii<\/p>\n<h2><strong>\u5c06\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e<\/strong><\/h2>\n<p>hex_str = &quot;48656c6c6f20576f726c64&quot;<\/p>\n<p>bin_data = binascii.unhexlify(hex_str)<\/p>\n<p>print(bin_data)  # \u8f93\u51fa b&#39;Hello World&#39;<\/p>\n<h2><strong>\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>bin_data = b&quot;Hello World&quot;<\/p>\n<p>hex_str = binascii.hexlify(bin_data).decode()<\/p>\n<p>print(hex_str)  # \u8f93\u51fa 48656c6c6f20576f726c64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u8fdb\u9636\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u5341\u516d\u8fdb\u5236\u6570\u7ecf\u5e38\u7528\u4e8e\u8868\u793a\u989c\u8272\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.new(&quot;RGB&quot;, (100, 100), &quot;#FF5733&quot;)<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5728\u5bc6\u7801\u5b66\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5bc6\u7801\u5b66\u4e2d\uff0c\u5341\u516d\u8fdb\u5236\u6570\u5e38\u7528\u4e8e\u8868\u793a\u5bc6\u94a5\u548c\u52a0\u5bc6\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import hashlib<\/p>\n<h2><strong>\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684SHA-256\u54c8\u5e0c\u503c<\/strong><\/h2>\n<p>data = &quot;Hello World&quot;<\/p>\n<p>hash_obj = hashlib.sha256(data.encode())<\/p>\n<p>hex_digest = hash_obj.hexdigest()<\/p>\n<p>print(hex_digest)  # \u8f93\u51fa SHA-256 \u54c8\u5e0c\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>\u8f93\u5165\u65e0\u6548\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e0d\u662f\u6709\u6548\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c<code>int()<\/code>\u51fd\u6570\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    dec_num = int(&quot;G1&quot;, 16)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u9519\u8bef\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u5927\u5c0f\u5199\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u53ef\u4ee5\u662f\u5927\u5c0f\u5199\u6df7\u5408\u7684\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u7edf\u4e00\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_str = &quot;1aBcDeF&quot;<\/p>\n<p>hex_str = hex_str.lower()<\/p>\n<p>dec_num = int(hex_str, 16)<\/p>\n<p>print(dec_num)  # \u8f93\u51fa 28036591<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4fdd\u7559\u5341\u516d\u8fdb\u5236\u6570\u7684\u524d\u5bfc\u96f6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u4fdd\u7559\u5341\u516d\u8fdb\u5236\u6570\u7684\u524d\u5bfc\u96f6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dec_num = 26<\/p>\n<p>hex_str = f&quot;{dec_num:04x}&quot;<\/p>\n<p>print(hex_str)  # \u8f93\u51fa 001a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u4e00\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5904\u7406\u5341\u516d\u8fdb\u5236\u6570\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528<code>0x<\/code>\u524d\u7f00\u8868\u793a\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>int()<\/code>\u8fdb\u884c\u8f6c\u6362\u3001\u4f7f\u7528<code>hex()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u8fdb\u884c\u8f6c\u6362<\/strong>\uff0c\u90fd\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5341\u516d\u8fdb\u5236\u6570\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3001\u7f51\u7edc\u7f16\u7a0b\u3001\u5bc6\u7801\u5b66\u7b49\u9886\u57df\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u548c\u8f6c\u6362\u5341\u516d\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5728\u65e5\u5e38\u7f16\u7a0b\u4e2d\uff0c\u7406\u89e3\u548c\u638c\u63e1\u5341\u516d\u8fdb\u5236\u6570\u7684\u8f93\u5165\u548c\u5904\u7406\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u8fd8\u80fd\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u4e2d\u5904\u7406\u5341\u516d\u8fdb\u5236\u6570\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\u8f93\u5165\u5341\u516d\u8fdb\u5236\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u6765\u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u5341\u516d\u8fdb\u5236\u6570\u3002\u7528\u6237\u8f93\u5165\u7684\u5341\u516d\u8fdb\u5236\u6570\u901a\u5e38\u4ee5<code>0x<\/code>\u5f00\u5934\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165<code>0x1A<\/code>\uff0c\u53ef\u4ee5\u7528<code>int(input(&quot;\u8bf7\u8f93\u5165\u5341\u516d\u8fdb\u5236\u6570\uff1a&quot;), 16)<\/code>\u6765\u83b7\u53d6\u5176\u5341\u8fdb\u5236\u503c\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u5341\u516d\u8fdb\u5236\u6570\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u51fd\u6570\u6765\u5904\u7406\u5341\u516d\u8fdb\u5236\u6570\u3002\u5e38\u7528\u7684\u5305\u62ec<code>hex()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u5341\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u683c\u5f0f\uff0c<code>int()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5341\u516d\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff0c<code>format()<\/code>\u51fd\u6570\u4e5f\u53ef\u7528\u4e8e\u5c06\u6570\u5b57\u4ee5\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u8f93\u51fa\u3002\u8fd9\u4e9b\u51fd\u6570\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u6570\u636e\u8f6c\u6362\u548c\u683c\u5f0f\u5316\u65f6\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u5341\u516d\u8fdb\u5236\u6570\u6709\u6548\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u5341\u516d\u8fdb\u5236\u6570\u662f\u6709\u6548\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002\u901a\u8fc7\u5728\u5c1d\u8bd5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\u6355\u83b7<code>ValueError<\/code>\u5f02\u5e38\uff0c\u53ef\u4ee5\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6709\u6548\u7684\u5341\u516d\u8fdb\u5236\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u5341\u516d\u8fdb\u5236\u6570\uff1a&quot;)\ntry:\n    decimal_value = int(user_input, 16)\n    print(f&quot;\u5341\u8fdb\u5236\u503c\u4e3a\uff1a{decimal_value}&quot;)\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6709\u6548\u7684\u5341\u516d\u8fdb\u5236\u6570\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u907f\u514d\u56e0\u65e0\u6548\u8f93\u5165\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u8f93\u5165\u5341\u516d\u8fdb\u5236 \u5728Python\u4e2d\u8f93\u5165\u5341\u516d\u8fdb\u5236\u6570\uff0c\u53ef\u4ee5\u4f7f\u75280x\u524d\u7f00\u8868\u793a\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570int()\u8fdb\u884c [&hellip;]","protected":false},"author":3,"featured_media":1162715,"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\/1162704"}],"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=1162704"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162704\/revisions"}],"predecessor-version":[{"id":1162718,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162704\/revisions\/1162718"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1162715"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1162704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1162704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1162704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}