{"id":1135253,"date":"2025-01-08T21:23:24","date_gmt":"2025-01-08T13:23:24","guid":{"rendered":""},"modified":"2025-01-08T21:23:28","modified_gmt":"2025-01-08T13:23:28","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86bin%e8%bd%ac%e6%8d%a2%e6%88%90%e6%96%87%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135253.html","title":{"rendered":"python\u5982\u4f55\u5c06bin\u8f6c\u6362\u6210\u6587\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103948\/19beb5cb-84ff-4f0a-8939-f4e94d899559.webp\" alt=\"python\u5982\u4f55\u5c06bin\u8f6c\u6362\u6210\u6587\u5b57\" \/><\/p>\n<p><p> <strong>\u8981\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u6210\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u3001\u4f7f\u7528\u9002\u5f53\u7684\u5b57\u7b26\u7f16\u7801\u5c06\u5b57\u8282\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282<\/strong>\uff1a\u4f7f\u7528<code>int<\/code>\u51fd\u6570\u548c<code>to_bytes<\/code>\u65b9\u6cd5\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u636e\u3002<\/li>\n<li><strong>\u4f7f\u7528\u9002\u5f53\u7684\u5b57\u7b26\u7f16\u7801\u89e3\u7801<\/strong>\uff1a\u4f7f\u7528<code>decode<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e38\u7528\u7684\u7f16\u7801\u6709UTF-8\u3001ASCII\u7b49\u3002<\/li>\n<li><strong>\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/strong>\uff1a\u786e\u4fdd\u6355\u83b7\u5e76\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u5982\u7f16\u7801\u9519\u8bef\u6216\u65e0\u6548\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u9610\u8ff0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u672c\u6982\u5ff5\u4e0e\u51c6\u5907\u5de5\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u4ec0\u4e48\u662f\u4e8c\u8fdb\u5236\u4e0e\u6587\u672c<\/h4>\n<\/p>\n<p><p><strong>\u4e8c\u8fdb\u5236\u6570\u636e<\/strong>\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u6700\u57fa\u672c\u7684\u5f62\u5f0f\uff0c\u75310\u548c1\u7ec4\u6210\u3002<strong>\u6587\u672c\u6570\u636e<\/strong>\u5219\u662f\u4eba\u7c7b\u53ef\u8bfb\u7684\u5b57\u7b26\u548c\u7b26\u53f7\u3002\u5c06\u4e8c\u8fdb\u5236\u8f6c\u6362\u4e3a\u6587\u672c\uff0c\u5b9e\u9645\u4e0a\u662f\u5c06\u4e00\u4e320\u548c1\u6620\u5c04\u4e3a\u7279\u5b9a\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b57\u7b26\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u7f16\u7801\u662f\u5c06\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u6620\u5c04\u5230\u8ba1\u7b97\u673a\u53ef\u4ee5\u7406\u89e3\u7684\u4e8c\u8fdb\u5236\u6570\u3002\u5e38\u89c1\u7684\u5b57\u7b26\u7f16\u7801\u6709ASCII\u3001UTF-8\u548cUTF-16\u3002\u4e86\u89e3\u7f16\u7801\u7684\u539f\u7406\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5b8c\u6210\u4e8c\u8fdb\u5236\u5230\u6587\u672c\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<code>&#39;01001000 01100101 01101100 01101100 01101111&#39;<\/code>\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Python\u7684<code>int<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>int<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>to_bytes<\/code>\u65b9\u6cd5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bin_to_bytes(binary_str):<\/p>\n<p>    binary_values = binary_str.split(&#39; &#39;)<\/p>\n<p>    byte_array = bytearray()<\/p>\n<p>    for binary_value in binary_values:<\/p>\n<p>        integer = int(binary_value, 2)<\/p>\n<p>        byte_array.append(integer)<\/p>\n<p>    return byte_array<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">binary_str = &#39;01001000 01100101 01101100 01101100 01101111&#39;<\/p>\n<p>byte_data = bin_to_bytes(binary_str)<\/p>\n<p>print(byte_data)  # \u8f93\u51fa: bytearray(b&#39;Hello&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u7b26\u7f16\u7801\u89e3\u7801\u5b57\u8282<\/h3>\n<\/p>\n<p><p>\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u6587\u672c\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u9002\u5f53\u7684\u5b57\u7b26\u7f16\u7801\u3002\u5e38\u7528\u7684\u7f16\u7801\u6709UTF-8\u548cASCII\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>decode<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>decode<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u9002\u5f53\u7684\u7f16\u7801\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bytes_to_text(byte_data, encoding=&#39;utf-8&#39;):<\/p>\n<p>    try:<\/p>\n<p>        text = byte_data.decode(encoding)<\/p>\n<p>    except UnicodeDecodeError:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(f&quot;\u65e0\u6cd5\u4f7f\u7528\u7f16\u7801 {encoding} \u89e3\u7801\u5b57\u8282\u6570\u636e&quot;)<\/p>\n<p>    return text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = bytes_to_text(byte_data)<\/p>\n<p>print(text)  # \u8f93\u51fa: Hello<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u5230\u6587\u672c\u7684\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u65e0\u6548\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u6216\u7f16\u7801\u9519\u8bef\u3002\u9700\u8981\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u5904\u7406\u65e0\u6548\u7684\u4e8c\u8fdb\u5236\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u8f93\u5165\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u65e0\u6548\uff0c\u6211\u4eec\u9700\u8981\u6355\u83b7\u5e76\u5904\u7406<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bin_to_bytes_<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>(binary_str):<\/p>\n<p>    binary_values = binary_str.split(&#39; &#39;)<\/p>\n<p>    byte_array = bytearray()<\/p>\n<p>    for binary_value in binary_values:<\/p>\n<p>        try:<\/p>\n<p>            integer = int(binary_value, 2)<\/p>\n<p>            byte_array.append(integer)<\/p>\n<p>        except ValueError:<\/p>\n<p>            raise ValueError(f&quot;\u65e0\u6548\u7684\u4e8c\u8fdb\u5236\u6570\u636e: {binary_value}&quot;)<\/p>\n<p>    return byte_array<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    byte_data = bin_to_bytes_safe(&#39;01001000 01100101 01101100 01101100 01101111&#39;)<\/p>\n<p>    text = bytes_to_text(byte_data)<\/p>\n<p>    print(text)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u4e8c\u8fdb\u5236\u5230\u6587\u672c\u7684\u8f6c\u6362\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bin_to_text(binary_str, encoding=&#39;utf-8&#39;):<\/p>\n<p>    def bin_to_bytes(binary_str):<\/p>\n<p>        binary_values = binary_str.split(&#39; &#39;)<\/p>\n<p>        byte_array = bytearray()<\/p>\n<p>        for binary_value in binary_values:<\/p>\n<p>            try:<\/p>\n<p>                integer = int(binary_value, 2)<\/p>\n<p>                byte_array.append(integer)<\/p>\n<p>            except ValueError:<\/p>\n<p>                raise ValueError(f&quot;\u65e0\u6548\u7684\u4e8c\u8fdb\u5236\u6570\u636e: {binary_value}&quot;)<\/p>\n<p>        return byte_array<\/p>\n<p>    byte_data = bin_to_bytes(binary_str)<\/p>\n<p>    try:<\/p>\n<p>        text = byte_data.decode(encoding)<\/p>\n<p>    except UnicodeDecodeError:<\/p>\n<p>        raise ValueError(f&quot;\u65e0\u6cd5\u4f7f\u7528\u7f16\u7801 {encoding} \u89e3\u7801\u5b57\u8282\u6570\u636e&quot;)<\/p>\n<p>    return text<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>binary_str = &#39;01001000 01100101 01101100 01101100 01101111&#39;<\/p>\n<p>try:<\/p>\n<p>    text = bin_to_text(binary_str)<\/p>\n<p>    print(text)  # \u8f93\u51fa: Hello<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1. \u6587\u4ef6\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u6587\u672c\u3002\u4f8b\u5982\uff0c\u4ece\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u53ef\u8bfb\u6587\u672c\u3002<\/p>\n<\/p>\n<p><h4>2. \u7f51\u7edc\u901a\u4fe1<\/h4>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u4f20\u8f93\u3002\u5c06\u63a5\u6536\u5230\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u6587\u672c\u662f\u5e38\u89c1\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>3. \u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u5305\u542b\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u63d0\u53d6\u5e76\u8f6c\u6362\u4e3a\u53ef\u8bfb\u6587\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f18\u5316\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>1. \u652f\u6301\u66f4\u591a\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6269\u5c55\u51fd\u6570\u4ee5\u652f\u6301\u66f4\u591a\u5b57\u7b26\u7f16\u7801\uff0c\u5982ISO-8859-1\u3001UTF-16\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bin_to_text(binary_str, encoding=&#39;utf-8&#39;):<\/p>\n<p>    def bin_to_bytes(binary_str):<\/p>\n<p>        binary_values = binary_str.split(&#39; &#39;)<\/p>\n<p>        byte_array = bytearray()<\/p>\n<p>        for binary_value in binary_values:<\/p>\n<p>            try:<\/p>\n<p>                integer = int(binary_value, 2)<\/p>\n<p>                byte_array.append(integer)<\/p>\n<p>            except ValueError:<\/p>\n<p>                raise ValueError(f&quot;\u65e0\u6548\u7684\u4e8c\u8fdb\u5236\u6570\u636e: {binary_value}&quot;)<\/p>\n<p>        return byte_array<\/p>\n<p>    byte_data = bin_to_bytes(binary_str)<\/p>\n<p>    try:<\/p>\n<p>        text = byte_data.decode(encoding)<\/p>\n<p>    except UnicodeDecodeError:<\/p>\n<p>        raise ValueError(f&quot;\u65e0\u6cd5\u4f7f\u7528\u7f16\u7801 {encoding} \u89e3\u7801\u5b57\u8282\u6570\u636e&quot;)<\/p>\n<p>    return text<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>binary_str = &#39;01001000 01100101 01101100 01101100 01101111&#39;<\/p>\n<p>try:<\/p>\n<p>    text = bin_to_text(binary_str, encoding=&#39;utf-8&#39;)<\/p>\n<p>    print(text)  # \u8f93\u51fa: Hello<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6269\u5c55\u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u8be6\u7ec6\u7684\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\uff0c\u4ee5\u4fbf\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u66f4\u597d\u5730\u8c03\u8bd5\u548c\u8ffd\u8e2a\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.ERROR)<\/p>\n<p>def bin_to_text(binary_str, encoding=&#39;utf-8&#39;):<\/p>\n<p>    def bin_to_bytes(binary_str):<\/p>\n<p>        binary_values = binary_str.split(&#39; &#39;)<\/p>\n<p>        byte_array = bytearray()<\/p>\n<p>        for binary_value in binary_values:<\/p>\n<p>            try:<\/p>\n<p>                integer = int(binary_value, 2)<\/p>\n<p>                byte_array.append(integer)<\/p>\n<p>            except ValueError:<\/p>\n<p>                logging.error(f&quot;\u65e0\u6548\u7684\u4e8c\u8fdb\u5236\u6570\u636e: {binary_value}&quot;)<\/p>\n<p>                raise ValueError(f&quot;\u65e0\u6548\u7684\u4e8c\u8fdb\u5236\u6570\u636e: {binary_value}&quot;)<\/p>\n<p>        return byte_array<\/p>\n<p>    byte_data = bin_to_bytes(binary_str)<\/p>\n<p>    try:<\/p>\n<p>        text = byte_data.decode(encoding)<\/p>\n<p>    except UnicodeDecodeError:<\/p>\n<p>        logging.error(f&quot;\u65e0\u6cd5\u4f7f\u7528\u7f16\u7801 {encoding} \u89e3\u7801\u5b57\u8282\u6570\u636e&quot;)<\/p>\n<p>        raise ValueError(f&quot;\u65e0\u6cd5\u4f7f\u7528\u7f16\u7801 {encoding} \u89e3\u7801\u5b57\u8282\u6570\u636e&quot;)<\/p>\n<p>    return text<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>binary_str = &#39;01001000 01100101 01101100 01101100 01101111&#39;<\/p>\n<p>try:<\/p>\n<p>    text = bin_to_text(binary_str, encoding=&#39;utf-8&#39;)<\/p>\n<p>    print(text)  # \u8f93\u51fa: Hello<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u6210\u6587\u5b57\u3002\u5e0c\u671b\u8fd9\u4e9b\u4fe1\u606f\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e00\u8fc7\u7a0b\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\u6587\u4ef6\u8f6c\u6362\u4e3a\u53ef\u8bfb\u6587\u672c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u7136\u540e\u901a\u8fc7<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file.bin&#39;, &#39;rb&#39;) as file:\n    binary_data = file.read()\n    text = binary_data.decode(&#39;utf-8&#39;)  # \u5047\u8bbe\u4e8c\u8fdb\u5236\u6570\u636e\u662fUTF-8\u7f16\u7801\n<\/code><\/pre>\n<p>\u786e\u4fdd\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u4fbf\u6b63\u786e\u89e3\u6790\u6570\u636e\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5e93\u6765\u5904\u7406\u4e8c\u8fdb\u5236\u5230\u6587\u672c\u7684\u8f6c\u6362\u662f\u5426\u6709\u63a8\u8350\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u6709\u8bb8\u591a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u89e3\u6790\u590d\u6742\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u7ed3\u6784\u3002\u6b64\u5916\uff0c<code>binascii<\/code>\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3aASCII\u5b57\u7b26\u4e32\u7684\u529f\u80fd\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import binascii\n\nbinary_data = b&#39;\\x68\\x65\\x6c\\x6c\\x6f&#39;  # \u793a\u4f8b\u4e8c\u8fdb\u5236\u6570\u636e\ntext = binascii.unhexlify(binary_data).decode(&#39;utf-8&#39;)\nprint(text)  # \u8f93\u51fa &quot;hello&quot;\n<\/code><\/pre>\n<p><strong>\u5728\u8f6c\u6362\u4e8c\u8fdb\u5236\u6570\u636e\u4e3a\u6587\u672c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u54ea\u4e9b\u5e38\u89c1\u95ee\u9898\uff1f<\/strong><br \/>\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u95ee\u9898\u5305\u62ec\u7f16\u7801\u9519\u8bef\u548c\u6570\u636e\u635f\u574f\u3002\u5982\u679c\u4e8c\u8fdb\u5236\u6570\u636e\u4f7f\u7528\u4e86\u4e0d\u6b63\u786e\u7684\u7f16\u7801\uff0c<code>decode()<\/code>\u65b9\u6cd5\u53ef\u80fd\u4f1a\u5f15\u53d1<code>UnicodeDecodeError<\/code>\u3002\u786e\u4fdd\u5728\u8f6c\u6362\u524d\u786e\u8ba4\u6570\u636e\u6e90\u7684\u7f16\u7801\u683c\u5f0f\u3002\u6b64\u5916\uff0c\u8bfb\u53d6\u7684\u6587\u4ef6\u662f\u5426\u5b8c\u6574\u4e5f\u4f1a\u5f71\u54cd\u7ed3\u679c\uff0c\u56e0\u6b64\u5728\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u6ca1\u6709\u88ab\u622a\u65ad\u6216\u635f\u574f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u6210\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u3001\u4f7f\u7528\u9002\u5f53 [&hellip;]","protected":false},"author":3,"featured_media":1135260,"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\/1135253"}],"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=1135253"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135253\/revisions"}],"predecessor-version":[{"id":1135262,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135253\/revisions\/1135262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135260"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}