{"id":1040384,"date":"2024-12-31T12:40:44","date_gmt":"2024-12-31T04:40:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1040384.html"},"modified":"2024-12-31T12:40:46","modified_gmt":"2024-12-31T04:40:46","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e5%8c%96%e4%b8%ba%e4%ba%8c%e8%bf%9b%e5%88%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1040384.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u5316\u4e3a\u4e8c\u8fdb\u5236"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c6e693bd-c0e0-4f0d-8a22-833b84490d80.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8f6c\u5316\u4e3a\u4e8c\u8fdb\u5236\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5f62\u5f0f\u3002<strong>\u4f7f\u7528bin()\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4f4d\u8fd0\u7b97<\/strong>\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5728\u8fd9\u51e0\u79cd\u65b9\u6cd5\u4e2d\uff0cbin()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\uff0c\u5b83\u80fd\u591f\u5c06\u6574\u6570\u76f4\u63a5\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528bin()\u51fd\u6570\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u6574\u657010\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528bin(10)\u5f97\u5230&#39;0b1010&#39;\u3002\u5176\u4e2d&#39;0b&#39;\u662f\u4e8c\u8fdb\u5236\u7684\u524d\u7f00\uff0c&#39;1010&#39;\u662f10\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528bin()\u51fd\u6570<\/p>\n<\/p>\n<p><p>bin()\u51fd\u6570\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u5176\u8bed\u6cd5\u4e3a\uff1abin(x)\uff0c\u5176\u4e2dx\u662f\u8981\u8f6c\u6362\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 10<\/p>\n<p>binary = bin(num)<\/p>\n<p>print(binary)  # \u8f93\u51fa\uff1a0b1010<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> bin()\u51fd\u6570\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5927\u591a\u6570\u57fa\u672c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u53ea\u9002\u7528\u4e8e\u6574\u6570\uff0c\u65e0\u6cd5\u76f4\u63a5\u5904\u7406\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6d6e\u70b9\u6570\u6216\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528format()\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684format()\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u5176\u8bed\u6cd5\u4e3a\uff1aformat(value, format_spec)\uff0c\u5176\u4e2dvalue\u662f\u8981\u8f6c\u6362\u7684\u503c\uff0cformat_spec\u662f\u683c\u5f0f\u8bf4\u660e\u7b26\u3002\u4e8c\u8fdb\u5236\u683c\u5f0f\u8bf4\u660e\u7b26\u4e3a&#39;b&#39;\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 10<\/p>\n<p>binary = format(num, &#39;b&#39;)<\/p>\n<p>print(binary)  # \u8f93\u51fa\uff1a1010<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> format()\u51fd\u6570\u7075\u6d3b\uff0c\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u683c\u5f0f\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981\u638c\u63e1\u683c\u5f0f\u8bf4\u660e\u7b26\uff0c\u53ef\u80fd\u5bf9\u521d\u5b66\u8005\u4e0d\u592a\u53cb\u597d\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528f-string<\/p>\n<\/p>\n<p><p>Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u5f15\u5165\u4e86f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u3002\u53ef\u4ee5\u5728f-string\u4e2d\u4f7f\u7528\u683c\u5f0f\u8bf4\u660e\u7b26&#39;b&#39;\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 10<\/p>\n<p>binary = f&#39;{num:b}&#39;<\/p>\n<p>print(binary)  # \u8f93\u51fa\uff1a1010<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> f-string\u7b80\u6d01\u76f4\u89c2\uff0c\u9002\u5408\u8fdb\u884c\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u4ec5\u9002\u7528\u4e8ePython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u4f4d\u8fd0\u7b97<\/p>\n<\/p>\n<p><p>\u4f4d\u8fd0\u7b97\u662f\u4e00\u79cd\u5e95\u5c42\u64cd\u4f5c\uff0c\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u4e8c\u8fdb\u5236\u4f4d\u3002\u53ef\u4ee5\u901a\u8fc7\u4f4d\u8fd0\u7b97\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def int_to_bin(n):<\/p>\n<p>    binary = &#39;&#39;<\/p>\n<p>    while n &gt; 0:<\/p>\n<p>        binary = str(n % 2) + binary<\/p>\n<p>        n = n \/\/ 2<\/p>\n<p>    return binary<\/p>\n<p>num = 10<\/p>\n<p>binary = int_to_bin(num)<\/p>\n<p>print(binary)  # \u8f93\u51fa\uff1a1010<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u4f4d\u8fd0\u7b97\u9002\u5408\u9700\u8981\u5bf9\u4e8c\u8fdb\u5236\u4f4d\u8fdb\u884c\u7cbe\u7ec6\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u4ee3\u7801\u8f83\u590d\u6742\uff0c\u4e0d\u5982\u5185\u7f6e\u51fd\u6570\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236<\/p>\n<\/p>\n<p><p>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u8f83\u4e3a\u590d\u6742\uff0c\u53ef\u4ee5\u4f7f\u7528struct\u6a21\u5757\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u518d\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>def float_to_bin(f):<\/p>\n<p>    [d] = struct.unpack(&quot;&gt;Q&quot;, struct.pack(&quot;&gt;d&quot;, f))<\/p>\n<p>    return f&#39;{d:064b}&#39;<\/p>\n<p>num = 10.5<\/p>\n<p>binary = float_to_bin(num)<\/p>\n<p>print(binary)  # \u8f93\u51fa\uff1a0100000000100101000000000000000000000000000000000000000000000000<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u53ef\u4ee5\u5904\u7406\u6d6e\u70b9\u6570\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u4ee3\u7801\u590d\u6742\uff0c\u6d89\u53ca\u5230\u5b57\u8282\u548c\u7ed3\u6784\u4f53\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236<\/p>\n<\/p>\n<p><p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u53ef\u4ee5\u901a\u8fc7\u9010\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5176ASCII\u7801\uff0c\u518d\u5c06ASCII\u7801\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def str_to_bin(s):<\/p>\n<p>    return &#39; &#39;.join(format(ord(c), &#39;08b&#39;) for c in s)<\/p>\n<p>text = &quot;Hello&quot;<\/p>\n<p>binary = str_to_bin(text)<\/p>\n<p>print(binary)  # \u8f93\u51fa\uff1a01001000 01100101 01101100 01101100 01101111<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u53ef\u4ee5\u5904\u7406\u5b57\u7b26\u4e32\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981\u9010\u4e2a\u5b57\u7b26\u5904\u7406\uff0c\u4ee3\u7801\u8f83\u957f\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5f62\u5f0f\u3002<strong>bin()\u51fd\u6570\u3001format()\u51fd\u6570\u3001f-string\u3001\u4f4d\u8fd0\u7b97<\/strong>\u662f\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4e0d\u540c\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u548c\u573a\u666f\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Python\u4e2d\u8fdb\u884c\u4e8c\u8fdb\u5236\u8f6c\u6362\u64cd\u4f5c\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u611f\u8c22\u9605\u8bfb\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u8981\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>bin()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6574\u6570\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u4ee5&#39;0b&#39;\u5f00\u5934\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>bin(10)<\/code>\u5c06\u8fd4\u56de<code>&#39;0b1010&#39;<\/code>\u3002\u5982\u679c\u60a8\u53ea\u60f3\u8981\u4e8c\u8fdb\u5236\u6570\u5b57\u672c\u8eab\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247<code>bin(10)[2:]<\/code>\u6765\u83b7\u53d6<code>&#39;1010&#39;<\/code>\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u53ea\u9700\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u5e76\u6307\u5b9a\u57fa\u6570\u4e3a2\u3002\u4f8b\u5982\uff0c<code>int(&#39;1010&#39;, 2)<\/code>\u5c06\u8fd4\u56de\u6574\u657010\u3002\u786e\u4fdd\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u7b26\u5408\u4e8c\u8fdb\u5236\u683c\u5f0f\uff08\u53ea\u5305\u542b0\u548c1\uff09\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u6574\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1f<\/strong><br \/>\u5904\u7406\u8d1f\u6574\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8865\u7801\u8868\u793a\u6cd5\u3002Python\u7684<code>bin()<\/code>\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u7528\u4e8e\u8d1f\u6574\u6570\uff0c\u4f8b\u5982\uff0c<code>bin(-10)<\/code>\u5c06\u8fd4\u56de<code>&#39;-0b1010&#39;<\/code>\u3002\u5982\u679c\u60a8\u9700\u8981\u83b7\u53d6\u8d1f\u6570\u7684\u8865\u7801\u5f62\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5148\u5c06\u5176\u7edd\u5bf9\u503c\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff0c\u7136\u540e\u6839\u636e\u4f4d\u6570\u548c\u7b26\u53f7\u4f4d\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\uff0c\u901a\u5e38\u6d89\u53ca\u5230\u4f4d\u8fd0\u7b97\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4ee5\u56fa\u5b9a\u957f\u5ea6\u8f93\u51fa\u4e8c\u8fdb\u5236\u6570\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u63a7\u5236\u8f93\u51fa\u7684\u4e8c\u8fdb\u5236\u6570\u7684\u957f\u5ea6\u3002\u5229\u7528<code>format()<\/code>\u51fd\u6570\u6216\u8005f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u7684\u5bbd\u5ea6\u3002\u4f8b\u5982\uff0c<code>format(10, &#39;05b&#39;)<\/code>\u5c06\u8fd4\u56de<code>&#39;01010&#39;<\/code>\uff0c\u5373\u5728\u4e8c\u8fdb\u5236\u6570\u524d\u8865\u96f6\u4ee5\u8fbe\u52305\u4f4d\u957f\u5ea6\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u4e8c\u8fdb\u5236\u6570\u5728\u5bf9\u9f50\u6216\u5b58\u50a8\u65f6\u7b26\u5408\u7279\u5b9a\u7684\u683c\u5f0f\u8981\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5f62\u5f0f\u3002\u4f7f\u7528bin()\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1040393,"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\/1040384"}],"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=1040384"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1040384\/revisions"}],"predecessor-version":[{"id":1040395,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1040384\/revisions\/1040395"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1040393"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1040384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1040384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1040384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}