{"id":1122083,"date":"2025-01-08T19:16:47","date_gmt":"2025-01-08T11:16:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122083.html"},"modified":"2025-01-08T19:16:49","modified_gmt":"2025-01-08T11:16:49","slug":"python%e5%a6%82%e4%bd%95%e6%a0%bc%e5%bc%8f%e5%8c%9616%e8%bf%9b%e5%88%b6%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1122083.html","title":{"rendered":"python\u5982\u4f55\u683c\u5f0f\u531616\u8fdb\u5236\u7684\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084101\/dec02864-368c-468c-a7eb-2a2f6ab339e2.webp\" alt=\"python\u5982\u4f55\u683c\u5f0f\u531616\u8fdb\u5236\u7684\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u683c\u5f0f\u531616\u8fdb\u5236\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u7684<code>hex()<\/code>\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001<code>f-string<\/code>\u3001\u4ee5\u53ca<code>struct<\/code>\u6a21\u5757\u3002<\/strong> \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c<code>f-string<\/code>\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u7075\u6d3b\u4e14\u6613\u8bfb\u7684\u683c\u5f0f\u5316\u80fd\u529b\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>hex()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>hex()<\/code>\u51fd\u6570\u662f\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a16\u8fdb\u5236\u5b57\u7b26\u4e32\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6574\u6570\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u4ee5&#39;0x&#39;\u5f00\u5934\u768416\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">number = 255<\/p>\n<p>hex_representation = hex(number)<\/p>\n<p>print(hex_representation)  # \u8f93\u51fa\uff1a0xff<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><code>hex()<\/code>\u51fd\u6570\u975e\u5e38\u65b9\u4fbf\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5feb\u901f\u67e5\u770b\u6574\u6570\u768416\u8fdb\u5236\u8868\u793a\u65f6\u3002\u7136\u800c\uff0c\u5b83\u7684\u8f93\u51fa\u683c\u5f0f\u56fa\u5b9a\uff0c\u5e26\u6709&#39;0x&#39;\u524d\u7f00\uff0c\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u683c\u5f0f\u6216\u8005\u53bb\u6389\u524d\u7f00\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 255<\/p>\n<p>hex_representation = hex(number)[2:]  # \u53bb\u6389&#39;0x&#39;\u524d\u7f00<\/p>\n<p>print(hex_representation)  # \u8f93\u51fa\uff1aff<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u6cd5\u6765\u683c\u5f0f\u531616\u8fdb\u5236\u6570\u636e\u3002\u4f7f\u7528<code>%x<\/code>\u6216<code>%X<\/code>\uff0c\u53ef\u4ee5\u5c06\u6574\u6570\u683c\u5f0f\u5316\u4e3a\u5c0f\u5199\u6216\u5927\u5199\u768416\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">number = 255<\/p>\n<p>hex_representation = &quot;%x&quot; % number<\/p>\n<p>print(hex_representation)  # \u8f93\u51fa\uff1aff<\/p>\n<p>hex_representation_upper = &quot;%X&quot; % number<\/p>\n<p>print(hex_representation_upper)  # \u8f93\u51fa\uff1aFF<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u53ef\u4ee5\u6307\u5b9a\u6700\u5c0f\u5bbd\u5ea6\u3001\u586b\u5145\u5b57\u7b26\u7b49\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u9700\u8981\u5bf9\u9f50\u6216\u7279\u5b9a\u683c\u5f0f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 255<\/p>\n<p>hex_representation = &quot;%04x&quot; % number  # \u8f93\u51fa\uff1a00ff\uff0c\u81f3\u5c114\u4f4d\uff0c\u524d\u9762\u88650<\/p>\n<p>print(hex_representation)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528<code>f-string<\/code><\/h2>\n<\/p>\n<p><p><code>f-string<\/code>\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4ee5\u5176\u7b80\u6d01\u548c\u76f4\u89c2\u8457\u79f0\u3002\u4f7f\u7528<code>f-string<\/code>\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u683c\u5f0f\u531616\u8fdb\u5236\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">number = 255<\/p>\n<p>hex_representation = f&quot;{number:x}&quot;<\/p>\n<p>print(hex_representation)  # \u8f93\u51fa\uff1aff<\/p>\n<p>hex_representation_upper = f&quot;{number:X}&quot;<\/p>\n<p>print(hex_representation_upper)  # \u8f93\u51fa\uff1aFF<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><code>f-string<\/code>\u4e0d\u4ec5\u652f\u6301\u57fa\u672c\u7684\u683c\u5f0f\u5316\uff0c\u8fd8\u652f\u6301\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u8fd0\u7b97\u548c\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 255<\/p>\n<p>hex_representation = f&quot;{number:04x}&quot;  # \u8f93\u51fa\uff1a00ff\uff0c\u81f3\u5c114\u4f4d\uff0c\u524d\u9762\u88650<\/p>\n<p>print(hex_representation)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528<code>struct<\/code>\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>struct<\/code>\u6a21\u5757\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5e38\u7528\u4e8e\u9700\u8981\u5c06\u6570\u636e\u6253\u5305\u6210\u56fa\u5b9a\u683c\u5f0f\u7684\u5b57\u8282\u5e8f\u5217\u7684\u573a\u666f\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u531616\u8fdb\u5236\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>number = 255<\/p>\n<p>hex_representation = struct.pack(&#39;&gt;I&#39;, number).hex()<\/p>\n<p>print(hex_representation)  # \u8f93\u51fa\uff1a000000ff<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><code>struct.pack()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6574\u6570\u6253\u5305\u6210\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u8282\u5e8f\u5217\uff0c\u7136\u540e\u4f7f\u7528<code>hex()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a16\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 255<\/p>\n<p>hex_representation = struct.pack(&#39;&gt;I&#39;, number).hex()<\/p>\n<p>print(hex_representation)  # \u8f93\u51fa\uff1a000000ff<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h2>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e00\uff1a\u683c\u5f0f\u5316\u5e76\u6253\u5370\u4e00\u7ec4\u6570\u636e<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 255]<\/p>\n<p>hex_representations = [f&quot;{number:x}&quot; for number in numbers]<\/p>\n<p>print(hex_representations)  # \u8f93\u51fa\uff1a[&#39;a&#39;, &#39;14&#39;, &#39;1e&#39;, &#39;ff&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e8c\uff1a\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 255]<\/p>\n<p>with open(&#39;hex_data.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for number in numbers:<\/p>\n<p>        file.write(f&quot;{number:04x}\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e09\uff1a\u8bfb\u53d6\u5e76\u89e3\u6790\u6570\u636e<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;hex_data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        number = int(line.strip(), 16)<\/p>\n<p>        print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u683c\u5f0f\u531616\u8fdb\u5236\u6570\u636e\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong><code>hex()<\/code>\u51fd\u6570\u7b80\u6d01\u76f4\u89c2\uff0c\u9002\u5408\u5feb\u901f\u8f6c\u6362\uff1b\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7075\u6d3b\u591a\u6837\uff0c\u9002\u5408\u9700\u8981\u7279\u5b9a\u683c\u5f0f\u7684\u573a\u666f\uff1b<code>f-string<\/code>\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u5408\u73b0\u4ee3Python\u7f16\u7a0b\uff1b<code>struct<\/code>\u6a21\u5757\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\u6765\u683c\u5f0f\u531616\u8fdb\u5236\u6570\u636e\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5c06\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u5c06\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>int(&#39;1A&#39;, 16)<\/code>\u5c06\u8fd4\u56de26\uff0c&#39;1A&#39;\u662f\u5341\u516d\u8fdb\u5236\u8868\u793a\u3002\u786e\u4fdd\u5b57\u7b26\u4e32\u524d\u6ca1\u6709\u591a\u4f59\u7684\u7a7a\u683c\u548c\u5b57\u7b26\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u683c\u5f0f\u5316\u5341\u516d\u8fdb\u5236\u6570\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6216\u8005<code>format()<\/code>\u51fd\u6570\u6765\u683c\u5f0f\u5316\u5341\u516d\u8fdb\u5236\u6570\u7684\u8f93\u51fa\u3002\u6bd4\u5982\u4f7f\u7528<code>format(255, &#39;x&#39;)<\/code>\u5c06\u8fd4\u56de&#39;ff&#39;\uff0c\u800c\u4f7f\u7528<code>f&#39;{255:x}&#39;<\/code>\u4e5f\u80fd\u5b9e\u73b0\u76f8\u540c\u6548\u679c\u3002\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\u63a7\u5236\u8f93\u51fa\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5341\u516d\u8fdb\u5236\u6570\u8f6c\u5316\u4e3a\u5b57\u8282\u6570\u636e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>bytes.fromhex()<\/code>\u65b9\u6cd5\u5c06\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u636e\u3002\u4f8b\u5982\uff0c<code>bytes.fromhex(&#39;2ef0&#39;)<\/code>\u5c06\u8fd4\u56de<code>b&#39;.\\xf0&#39;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06\u5341\u516d\u8fdb\u5236\u8868\u793a\u7684\u6570\u636e\u8f6c\u5316\u4e3a\u5b57\u8282\u6d41\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8f83\u957f\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u8fdb\u884c\u683c\u5f0f\u5316\uff1f<\/strong><br \/>\u5bf9\u4e8e\u8f83\u957f\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u548c\u5faa\u73af\u7ed3\u5408<code>format()<\/code>\u6216<code>f-string<\/code>\u8fdb\u884c\u5206\u6bb5\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5206\u6210\u6bcf\u4e24\u4e2a\u5b57\u7b26\u4e00\u7ec4\uff0c\u4ee5\u4fbf\u66f4\u6e05\u6670\u5730\u663e\u793a\u3002\u4f7f\u7528<code>&#39; &#39;.join([hex_string[i:i+2] for i in range(0, len(hex_string), 2)])<\/code>\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u79cd\u6548\u679c\uff0c\u4f7f\u8f93\u51fa\u66f4\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u683c\u5f0f\u531616\u8fdb\u5236\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u7684hex()\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001f-string\u3001\u4ee5\u53castru [&hellip;]","protected":false},"author":3,"featured_media":1122090,"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\/1122083"}],"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=1122083"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122083\/revisions"}],"predecessor-version":[{"id":1122091,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122083\/revisions\/1122091"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1122090"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1122083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1122083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1122083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}