{"id":1144136,"date":"2025-01-08T22:57:03","date_gmt":"2025-01-08T14:57:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144136.html"},"modified":"2025-01-08T22:57:06","modified_gmt":"2025-01-08T14:57:06","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e4%ba%8c%e8%bf%9b%e5%88%b6%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1144136.html","title":{"rendered":"python\u5982\u4f55\u7528\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\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\/24181401\/279c99ca-04db-4e53-a404-db7143dcb6c6.webp\" alt=\"python\u5982\u4f55\u7528\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u7528\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u624b\u52a8\u8f6c\u6362\u3001\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>Python\u8bed\u8a00\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u4e8c\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u6570\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u624b\u52a8\u8f6c\u6362\u3001\u9519\u8bef\u5904\u7406<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u5982<code>int()<\/code>\u548c<code>hex()<\/code>\u3002\u624b\u52a8\u8f6c\u6362\u5219\u9700\u8981\u901a\u8fc7\u6570\u5b66\u8ba1\u7b97\u548c\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002\u9519\u8bef\u5904\u7406\u540c\u6837\u91cd\u8981\uff0c\u786e\u4fdd\u8f93\u5165\u7684\u4e8c\u8fdb\u5236\u6570\u662f\u5408\u6cd5\u4e14\u6b63\u786e\u7684\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5b8c\u6210\u4e8c\u8fdb\u5236\u5230\u5341\u516d\u8fdb\u5236\u7684\u8f6c\u6362\u3002\u8fd9\u4e9b\u51fd\u6570\u5305\u62ec<code>int()<\/code>\u548c<code>hex()<\/code>\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>int()<\/code>\u548c<code>hex()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684<code>int()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6574\u6570\uff0c\u800c<code>hex()<\/code>\u51fd\u6570\u5219\u53ef\u4ee5\u5c06\u5341\u8fdb\u5236\u6574\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32<\/p>\n<p>binary_str = &#39;11011011&#39;<\/p>\n<p>decimal_int = int(binary_str, 2)  # \u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6574\u6570<\/p>\n<p>hex_str = hex(decimal_int)  # \u5c06\u5341\u8fdb\u5236\u6574\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32<\/p>\n<p>print(hex_str)  # \u8f93\u51fa\uff1a0xdb<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6574\u6570\uff0c\u7136\u540e\u4f7f\u7528<code>hex()<\/code>\u51fd\u6570\u5c06\u5341\u8fdb\u5236\u6574\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u8f93\u51fa\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u4ee5\u201c0x\u201d\u5f00\u5934\uff0c\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<p><h3>2. \u53bb\u6389\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u524d\u7f00<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u9700\u8981\u201c0x\u201d\u524d\u7f00\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u53bb\u6389\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_str_without_prefix = hex(decimal_int)[2:]<\/p>\n<p>print(hex_str_without_prefix)  # \u8f93\u51fa\uff1adb<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5207\u7247\u64cd\u4f5c<code>[2:]<\/code>\u53bb\u6389\u524d\u4e24\u4e2a\u5b57\u7b26\u201c0x\u201d\uff0c\u5f97\u5230\u7eaf\u7cb9\u7684\u5341\u516d\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u624b\u52a8\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u624b\u52a8\u8fdb\u884c\u4e8c\u8fdb\u5236\u5230\u5341\u516d\u8fdb\u5236\u7684\u8f6c\u6362\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u8f83\u4e3a\u590d\u6742\uff0c\u4f46\u6709\u52a9\u4e8e\u7406\u89e3\u8f6c\u6362\u7684\u539f\u7406\u3002<\/p>\n<\/p>\n<p><h3>1. \u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u5206\u7ec4<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u4ece\u53f3\u5411\u5de6\u6bcf\u56db\u4e2a\u4e00\u7ec4\u8fdb\u884c\u5206\u7ec4\uff0c\u4e0d\u8db3\u56db\u4f4d\u7684\u524d\u9762\u8865\u96f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">binary_str = &#39;11011011&#39;<\/p>\n<p>grouped_binary_str = binary_str.zfill((len(binary_str) + 3) \/\/ 4 * 4)  # \u8865\u96f6\u4f7f\u957f\u5ea6\u4e3a4\u7684\u500d\u6570<\/p>\n<p>print(grouped_binary_str)  # \u8f93\u51fa\uff1a11011011<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5efa\u7acb\u4e8c\u8fdb\u5236\u5230\u5341\u516d\u8fdb\u5236\u7684\u6620\u5c04\u8868<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u5efa\u7acb\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5230\u5341\u516d\u8fdb\u5236\u7684\u6620\u5c04\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">binary_to_hex_map = {<\/p>\n<p>    &#39;0000&#39;: &#39;0&#39;, &#39;0001&#39;: &#39;1&#39;, &#39;0010&#39;: &#39;2&#39;, &#39;0011&#39;: &#39;3&#39;,<\/p>\n<p>    &#39;0100&#39;: &#39;4&#39;, &#39;0101&#39;: &#39;5&#39;, &#39;0110&#39;: &#39;6&#39;, &#39;0111&#39;: &#39;7&#39;,<\/p>\n<p>    &#39;1000&#39;: &#39;8&#39;, &#39;1001&#39;: &#39;9&#39;, &#39;1010&#39;: &#39;A&#39;, &#39;1011&#39;: &#39;B&#39;,<\/p>\n<p>    &#39;1100&#39;: &#39;C&#39;, &#39;1101&#39;: &#39;D&#39;, &#39;1110&#39;: &#39;E&#39;, &#39;1111&#39;: &#39;F&#39;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8f6c\u6362\u6bcf\u7ec4\u4e8c\u8fdb\u5236\u6570<\/h3>\n<\/p>\n<p><p>\u7136\u540e\u5c06\u6bcf\u7ec4\u4e8c\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5341\u516d\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_str = &#39;&#39;.join(binary_to_hex_map[grouped_binary_str[i:i+4]] for i in range(0, len(grouped_binary_str), 4))<\/p>\n<p>print(hex_str)  # \u8f93\u51fa\uff1aDB<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u624b\u52a8\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u9519\u8bef\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8f93\u5165\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u975e\u6cd5\u5b57\u7b26\u6216\u683c\u5f0f\u9519\u8bef\uff0c\u56e0\u6b64\u9700\u8981\u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>1. \u68c0\u67e5\u8f93\u5165\u662f\u5426\u5408\u6cd5<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u68c0\u67e5\u8f93\u5165\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u662f\u5426\u5408\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_valid_binary(binary_str):<\/p>\n<p>    return re.fullmatch(r&#39;[01]+&#39;, binary_str) is not None<\/p>\n<p>binary_str = &#39;11011011&#39;<\/p>\n<p>if not is_valid_binary(binary_str):<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Invalid binary string&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6355\u83b7\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u9700\u8981\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    decimal_int = int(binary_str, 2)<\/p>\n<p>    hex_str = hex(decimal_int)[2:]<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;Error converting binary to hex: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6355\u83b7<code>ValueError<\/code>\u5f02\u5e38\uff0c\u53ef\u4ee5\u5728\u8f93\u5165\u4e0d\u5408\u6cd5\u6216\u8f6c\u6362\u5931\u8d25\u65f6\u8fdb\u884c\u9002\u5f53\u5904\u7406\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u7efc\u5408\u793a\u4f8b<\/h2>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u7ed3\u5408\u4e0a\u8ff0\u65b9\u6cd5\u5b9e\u73b0\u4e8c\u8fdb\u5236\u5230\u5341\u516d\u8fdb\u5236\u7684\u8f6c\u6362\uff0c\u5e76\u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_valid_binary(binary_str):<\/p>\n<p>    return re.fullmatch(r&#39;[01]+&#39;, binary_str) is not None<\/p>\n<p>def binary_to_hex(binary_str):<\/p>\n<p>    if not is_valid_binary(binary_str):<\/p>\n<p>        raise ValueError(&quot;Invalid binary string&quot;)<\/p>\n<p>    # \u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u8f6c\u6362<\/p>\n<p>    decimal_int = int(binary_str, 2)<\/p>\n<p>    hex_str = hex(decimal_int)[2:]<\/p>\n<p>    return hex_str.upper()  # \u8fd4\u56de\u5927\u5199\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32<\/p>\n<p>binary_str = &#39;11011011&#39;<\/p>\n<p>try:<\/p>\n<p>    hex_str = binary_to_hex(binary_str)<\/p>\n<p>    print(f&quot;Binary: {binary_str}, Hex: {hex_str}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u5e76\u8fdb\u884c\u5408\u6cd5\u6027\u68c0\u67e5\u548c\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u8fdb\u9636\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4e8c\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u7684\u8f6c\u6362\u53ef\u80fd\u4e0d\u4ec5\u9650\u4e8e\u7b80\u5355\u7684\u6570\u503c\u8f6c\u6362\uff0c\u8fd8\u53ef\u80fd\u6d89\u53ca\u5230\u6587\u4ef6\u5904\u7406\u3001\u7f51\u7edc\u7f16\u7a0b\u7b49\u590d\u6742\u573a\u666f\u3002\u4e0b\u9762\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u8fdb\u9636\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>1. \u6587\u4ef6\u5904\u7406\u4e2d\u7684\u4e8c\u8fdb\u5236\u4e0e\u5341\u516d\u8fdb\u5236\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\u5e76\u8fdb\u884c\u5341\u516d\u8fdb\u5236\u663e\u793a\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>struct<\/code>\u6a21\u5757\u6765\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\u5e76\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>def read_binary_file(file_path):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        binary_data = file.read()<\/p>\n<p>        hex_str = &#39;&#39;.join(f&#39;{byte:02X}&#39; for byte in binary_data)<\/p>\n<p>        return hex_str<\/p>\n<p>file_path = &#39;example.bin&#39;<\/p>\n<p>hex_str = read_binary_file(file_path)<\/p>\n<p>print(hex_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u4e8c\u8fdb\u5236\u4e0e\u5341\u516d\u8fdb\u5236\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u4f20\u8f93\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u4ee5\u4fbf\u8c03\u8bd5\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u53d1\u9001\u4e8c\u8fdb\u5236\u6570\u636e\u5e76\u6253\u5370\u5341\u516d\u8fdb\u5236\u8868\u793a<\/strong><\/h2>\n<p>def send_binary_data(data):<\/p>\n<p>    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:<\/p>\n<p>        s.connect((&#39;example.com&#39;, 80))<\/p>\n<p>        s.sendall(data)<\/p>\n<p>        hex_str = &#39;&#39;.join(f&#39;{byte:02X}&#39; for byte in data)<\/p>\n<p>        print(f&#39;Sent data (hex): {hex_str}&#39;)<\/p>\n<p>binary_data = b&#39;\\xDE\\xAD\\xBE\\xEF&#39;<\/p>\n<p>send_binary_data(binary_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u5185\u7f6e\u7684\u51fd\u6570\u548c\u6a21\u5757\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u7b80\u5316\u4e8c\u8fdb\u5236\u4e0e\u5341\u516d\u8fdb\u5236\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff0c<code>binascii<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import binascii<\/p>\n<p>binary_data = b&#39;\\xDE\\xAD\\xBE\\xEF&#39;<\/p>\n<p>hex_str = binascii.hexlify(binary_data).decode(&#39;utf-8&#39;)<\/p>\n<p>print(hex_str)  # \u8f93\u51fa\uff1adeadbeef<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u8fdb\u9636\u5e94\u7528\uff0c\u53ef\u4ee5\u770b\u5230\u4e8c\u8fdb\u5236\u4e0e\u5341\u516d\u8fdb\u5236\u8f6c\u6362\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u6027\u548c\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u6027\u80fd\u4f18\u5316<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e9b\u4f18\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u5199\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u8f6c\u6362\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">binary_data = b&#39;\\xDE\\xAD\\xBE\\xEF&#39;<\/p>\n<p>hex_str = &#39;&#39;.join([f&#39;{byte:02X}&#39; for byte in binary_data])<\/p>\n<p>print(hex_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u7f13\u5b58\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u9891\u7e41\u8fdb\u884c\u76f8\u540c\u8f6c\u6362\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u6765\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import functools<\/p>\n<p>@functools.lru_cache(maxsize=None)<\/p>\n<p>def binary_to_hex_cached(binary_str):<\/p>\n<p>    return binary_to_hex(binary_str)<\/p>\n<h2><strong>\u591a\u6b21\u8c03\u7528\u540c\u4e00\u8f6c\u6362<\/strong><\/h2>\n<p>binary_str = &#39;11011011&#39;<\/p>\n<p>print(binary_to_hex_cached(binary_str))<\/p>\n<p>print(binary_to_hex_cached(binary_str))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>functools.lru_cache<\/code>\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u7f13\u5b58\u8f6c\u6362\u7ed3\u679c\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>3. \u591a\u7ebf\u7a0b\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def convert_and_print(binary_str):<\/p>\n<p>    hex_str = binary_to_hex(binary_str)<\/p>\n<p>    print(f&quot;Binary: {binary_str}, Hex: {hex_str}&quot;)<\/p>\n<p>binary_strings = [&#39;11011011&#39;, &#39;10101010&#39;, &#39;11110000&#39;]<\/p>\n<p>threads = []<\/p>\n<p>for binary_str in binary_strings:<\/p>\n<p>    thread = threading.Thread(target=convert_and_print, args=(binary_str,))<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5e76\u884c\u5904\u7406\u591a\u4e2a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u63d0\u9ad8\u8f6c\u6362\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u624b\u52a8\u8f6c\u6362\u3001\u9519\u8bef\u5904\u7406<\/strong>\u3002\u540c\u65f6\uff0c\u7ed3\u5408\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff0c\u4ecb\u7ecd\u4e86\u5728\u6587\u4ef6\u5904\u7406\u3001\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u8f6c\u6362\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6027\u80fd\u4f18\u5316\u7684\u5efa\u8bae\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8bfb\u8005\u80fd\u591f\u6df1\u5165\u7406\u89e3\u4e8c\u8fdb\u5236\u4e0e\u5341\u516d\u8fdb\u5236\u8f6c\u6362\u7684\u539f\u7406\u548c\u5b9e\u73b0\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>int()<\/code>\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u518d\u4f7f\u7528<code>hex()<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u5177\u4f53\u6b65\u9aa4\u4e3a\uff1a  <\/p>\n<ol>\n<li>\u4f7f\u7528<code>int(binary_string, 2)<\/code>\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6574\u6570\u3002  <\/li>\n<li>\u4f7f\u7528<code>hex(decimal_integer)<\/code>\u5c06\u5341\u8fdb\u5236\u6574\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<br \/>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-python\">binary_string = &#39;1010&#39;  # \u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\ndecimal_integer = int(binary_string, 2)  # \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\nhex_string = hex(decimal_integer)  # \u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\nprint(hex_string)  # \u8f93\u51fa\u7ed3\u679c\u4e3a&#39;0xa&#39;\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u5c06\u4e8c\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u6570\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7<code>format()<\/code>\u51fd\u6570\u6216\u8005\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\u76f4\u63a5\u5c06\u4e8c\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>format()<\/code>\u51fd\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">binary_number = 0b1010  # \u4e8c\u8fdb\u5236\u6570\nhex_string = format(binary_number, &#39;x&#39;)  # \u76f4\u63a5\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\nprint(hex_string)  # \u8f93\u51fa\u7ed3\u679c\u4e3a&#39;a&#39;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u66f4\u52a0\u7b80\u6d01\u5730\u5b8c\u6210\u8f6c\u6362\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5e26\u6709\u524d\u7f00\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5f53\u5904\u7406\u5e26\u6709\u524d\u7f00\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff08\u4f8b\u5982&quot;0b1010&quot;\uff09\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664\u524d\u7f00\u540e\u518d\u8fdb\u884c\u8f6c\u6362\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">binary_string_with_prefix = &#39;0b1010&#39;  # \u5e26\u524d\u7f00\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\nbinary_string = binary_string_with_prefix[2:]  # \u53bb\u6389\u524d\u7f00\ndecimal_integer = int(binary_string, 2)  # \u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\nhex_string = hex(decimal_integer)  # \u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\nprint(hex_string)  # \u8f93\u51fa\u7ed3\u679c\u4e3a&#39;0xa&#39;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u5904\u7406\u65b9\u5f0f\u786e\u4fdd\u4e86\u8f93\u5165\u683c\u5f0f\u7684\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u7528\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u624b\u52a8\u8f6c\u6362\u3001\u9519\u8bef\u5904\u7406 Python\u8bed\u8a00\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u4e8c [&hellip;]","protected":false},"author":3,"featured_media":1144143,"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\/1144136"}],"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=1144136"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144136\/revisions"}],"predecessor-version":[{"id":1144146,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144136\/revisions\/1144146"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144143"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}