{"id":1103323,"date":"2025-01-08T16:09:11","date_gmt":"2025-01-08T08:09:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1103323.html"},"modified":"2025-01-08T16:09:14","modified_gmt":"2025-01-08T08:09:14","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86byte%e8%bd%ac%e6%8d%a2%e6%88%90int","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1103323.html","title":{"rendered":"python\u5982\u4f55\u5c06byte\u8f6c\u6362\u6210int"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065214\/f751747b-ccf0-44f5-9bca-f06ea021c3bc.webp\" alt=\"python\u5982\u4f55\u5c06byte\u8f6c\u6362\u6210int\" \/><\/p>\n<p><p> <strong>Python\u5c06byte\u8f6c\u6362\u6210int\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570int.from_bytes()\u3001\u4f7f\u7528struct\u6a21\u5757\u3001\u4f7f\u7528int()\u51fd\u6570\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528int.from_bytes()\u65b9\u6cd5\u6765\u5b8c\u6210\u8fd9\u4e2a\u8f6c\u6362\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>int.from_bytes()<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u548c\u5f3a\u5927\u7684\u51fd\u6570\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5c06\u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u662f <code>int.from_bytes(bytes, byteorder, *, signed=False)<\/code>\uff0c\u5176\u4e2d <code>bytes<\/code> \u662f\u9700\u8981\u8f6c\u6362\u7684\u5b57\u8282\u6570\u7ec4\uff0c<code>byteorder<\/code> \u53ef\u4ee5\u662f <code>big<\/code> \u6216 <code>little<\/code>\uff0c\u7528\u6765\u8868\u793a\u5b57\u8282\u7684\u5b58\u50a8\u987a\u5e8f\uff0c<code>signed<\/code> \u8868\u793a\u662f\u5426\u662f\u6709\u7b26\u53f7\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528int.from_bytes()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>int.from_bytes()<\/code> \u65b9\u6cd5\u662f\u5c06\u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u5b57\u8282\u987a\u5e8f\uff08<code>big<\/code> \u6216 <code>little<\/code>\uff09\u6765\u51b3\u5b9a\u5b57\u8282\u7684\u89e3\u91ca\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">byte_data = b&#39;\\x00\\x10&#39;<\/p>\n<p>result = int.from_bytes(byte_data, byteorder=&#39;big&#39;)<\/p>\n<p>print(result)  # \u8f93\u51fa 16<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b57\u8282 <code>b&#39;\\x00\\x10&#39;<\/code> \u88ab\u89e3\u91ca\u4e3a\u5927\u7aef\u5e8f\uff08big-endian\uff09\uff0c\u5e76\u8f6c\u6362\u4e3a\u6574\u6570 <code>16<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u5c0f\u7aef\u5e8f\u548c\u5927\u7aef\u5e8f<\/h4>\n<\/p>\n<p><p>\u5927\u7aef\u5e8f\uff08big-endian\uff09\u548c\u5c0f\u7aef\u5e8f\uff08little-endian\uff09\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u5b57\u8282\u5b58\u50a8\u987a\u5e8f\u3002\u5728\u5927\u7aef\u5e8f\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u5b57\u8282\u5b58\u50a8\u5728\u6700\u4f4e\u7684\u5185\u5b58\u5730\u5740\uff0c\u800c\u5728\u5c0f\u7aef\u5e8f\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u5b57\u8282\u5b58\u50a8\u5728\u6700\u9ad8\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">byte_data = b&#39;\\x00\\x10&#39;<\/p>\n<p>big_endian_result = int.from_bytes(byte_data, byteorder=&#39;big&#39;)<\/p>\n<p>little_endian_result = int.from_bytes(byte_data, byteorder=&#39;little&#39;)<\/p>\n<p>print(f&quot;Big endian: {big_endian_result}, Little endian: {little_endian_result}&quot;)<\/p>\n<h2><strong>\u8f93\u51fa Big endian: 16, Little endian: 4096<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6709\u7b26\u53f7\u6574\u6570<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>int.from_bytes()<\/code> \u5c06\u5b57\u8282\u6570\u7ec4\u89e3\u91ca\u4e3a\u65e0\u7b26\u53f7\u6574\u6570\u3002\u5982\u679c\u9700\u8981\u5c06\u5b57\u8282\u6570\u7ec4\u89e3\u91ca\u4e3a\u6709\u7b26\u53f7\u6574\u6570\uff0c\u53ef\u4ee5\u8bbe\u7f6e <code>signed=True<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">byte_data = b&#39;\\xff\\xff&#39;<\/p>\n<p>unsigned_result = int.from_bytes(byte_data, byteorder=&#39;big&#39;)<\/p>\n<p>signed_result = int.from_bytes(byte_data, byteorder=&#39;big&#39;, signed=True)<\/p>\n<p>print(f&quot;Unsigned: {unsigned_result}, Signed: {signed_result}&quot;)<\/p>\n<h2><strong>\u8f93\u51fa Unsigned: 65535, Signed: -1<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528struct\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>struct<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u79cd\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u65b9\u5f0f\u3002<code>struct<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6253\u5305\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u8fdb\u884c\u8f6c\u6362\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>struct.unpack()<\/code> \u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u8282\u6570\u7ec4\u89e3\u5305\u4e3a\u4e00\u4e2a\u5143\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u89e3\u91ca\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>byte_data = b&#39;\\x00\\x10&#39;<\/p>\n<p>result, = struct.unpack(&#39;&gt;H&#39;, byte_data)<\/p>\n<p>print(result)  # \u8f93\u51fa 16<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>&#39;&gt;H&#39;<\/code> \u8868\u793a\u4f7f\u7528\u5927\u7aef\u5e8f\uff08big-endian\uff09\u5c062\u4e2a\u5b57\u8282\u7684\u6570\u636e\u89e3\u91ca\u4e3a\u65e0\u7b26\u53f7\u77ed\u6574\u6570\uff08unsigned short\uff09\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u4e0d\u540c\u7684\u683c\u5f0f\u5b57\u7b26<\/h4>\n<\/p>\n<p><p><code>struct<\/code> \u6a21\u5757\u652f\u6301\u591a\u79cd\u683c\u5f0f\u5b57\u7b26\uff0c\u53ef\u4ee5\u8868\u793a\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u548c\u5b57\u8282\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>byte_data = b&#39;\\xff\\xff&#39;<\/p>\n<p>unsigned_result, = struct.unpack(&#39;&gt;H&#39;, byte_data)<\/p>\n<p>signed_result, = struct.unpack(&#39;&gt;h&#39;, byte_data)<\/p>\n<p>print(f&quot;Unsigned: {unsigned_result}, Signed: {signed_result}&quot;)<\/p>\n<h2><strong>\u8f93\u51fa Unsigned: 65535, Signed: -1<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>&#39;&gt;H&#39;<\/code> \u8868\u793a\u4f7f\u7528\u5927\u7aef\u5e8f\u5c06\u5b57\u8282\u89e3\u91ca\u4e3a\u65e0\u7b26\u53f7\u77ed\u6574\u6570\uff0c<code>&#39;&gt;h&#39;<\/code> \u8868\u793a\u4f7f\u7528\u5927\u7aef\u5e8f\u5c06\u5b57\u8282\u89e3\u91ca\u4e3a\u6709\u7b26\u53f7\u77ed\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528int()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7b80\u5355\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u6765\u5c06\u5b57\u8282\u6570\u636e\u89e3\u91ca\u4e3a\u6574\u6570\u3002\u4e0d\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5b57\u8282\u6570\u636e\u8868\u793a\u7684\u662f\u4e00\u4e2aASCII\u7801\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">byte_data = b&#39;1234&#39;<\/p>\n<p>result = int(byte_data)<\/p>\n<p>print(result)  # \u8f93\u51fa 1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5e76\u4e0d\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u6709\u5176\u5c40\u9650\u6027\uff0c\u4ec5\u9002\u7528\u4e8e\u5b57\u8282\u6570\u7ec4\u8868\u793a\u7684\u662f\u4e00\u4e2aASCII\u7801\u53ef\u8868\u793a\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06\u5b57\u8282\u8f6c\u6362\u4e3a\u6574\u6570\u5728Python\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\u3002\u4f7f\u7528 <code>int.from_bytes()<\/code> \u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u548c\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<code>struct<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u683c\u5f0f\u63a7\u5236\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u590d\u6742\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u573a\u666f\u3002<code>int()<\/code> \u51fd\u6570\u5219\u9002\u7528\u4e8e\u7b80\u5355\u7684ASCII\u7801\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u7406\u89e3\u5b57\u8282\u987a\u5e8f\u548c\u6709\u7b26\u53f7\/\u65e0\u7b26\u53f7\u7684\u6982\u5ff5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u5f97\u5b57\u8282\u548c\u6574\u6570\u4e4b\u95f4\u7684\u8f6c\u6362\u53d8\u5f97\u7b80\u5355\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int.from_bytes()<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u5b57\u8282\u5e8f\uff08&#39;big&#39;\u6216&#39;little&#39;\uff09\u548c\u53ef\u9009\u7684\u7b26\u53f7\u6807\u5fd7\u3002\u5b57\u8282\u5e8f\u51b3\u5b9a\u4e86\u591a\u5b57\u8282\u6574\u6570\u7684\u5b58\u50a8\u987a\u5e8f\uff0c\u800c\u7b26\u53f7\u6807\u5fd7\u5219\u6307\u793a\u6574\u6570\u662f\u5426\u4e3a\u8d1f\u6570\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684\u6574\u6570\u7684\u8303\u56f4\u662f\u591a\u5c11\uff1f<\/strong><br \/>\u8f6c\u6362\u540e\u7684\u6574\u6570\u8303\u56f4\u53d6\u51b3\u4e8e\u5b57\u8282\u5bf9\u8c61\u7684\u957f\u5ea6\u3002\u6bcf\u4e2a\u5b57\u8282\u5305\u542b8\u4f4d\uff0c\u56e0\u6b64\u4e00\u4e2an\u5b57\u8282\u7684\u5bf9\u8c61\u53ef\u4ee5\u8868\u793a\u7684\u6574\u6570\u8303\u56f4\u662f\u4ece0\u52302^(8<em>n)-1\u3002\u5982\u679c\u4f7f\u7528\u7b26\u53f7\u6807\u5fd7\uff0c\u8303\u56f4\u5c06\u53d8\u4e3a-2^(8<\/em>n-1)\u52302^(8*n-1)-1\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5b57\u8282\u6570\u636e\u65f6\uff0c\u6709\u4ec0\u4e48\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1f<\/strong><br \/>\u5b57\u8282\u5230\u6574\u6570\u7684\u8f6c\u6362\u901a\u5e38\u7528\u4e8e\u5904\u7406\u7f51\u7edc\u534f\u8bae\u3001\u6587\u4ef6\u683c\u5f0f\u89e3\u6790\u548c\u52a0\u5bc6\u7b49\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5f53\u63a5\u6536\u7f51\u7edc\u6570\u636e\u5305\u65f6\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u5b57\u8282\u5f62\u5f0f\u4f20\u8f93\uff0c\u89e3\u6790\u8fd9\u4e9b\u5b57\u8282\u4e3a\u6574\u6570\u53ef\u4ee5\u5e2e\u52a9\u7406\u89e3\u6570\u636e\u7684\u610f\u4e49\u3002\u53c8\u5982\uff0c\u6587\u4ef6\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u53ef\u80fd\u4ee5\u4e8c\u8fdb\u5236\u683c\u5f0f\u5b58\u50a8\uff0c\u8f6c\u6362\u4e3a\u6574\u6570\u540e\u53ef\u4ee5\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06byte\u8f6c\u6362\u6210int\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570int.from_bytes()\u3001\u4f7f\u7528struct [&hellip;]","protected":false},"author":3,"featured_media":1103334,"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\/1103323"}],"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=1103323"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103323\/revisions"}],"predecessor-version":[{"id":1103335,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103323\/revisions\/1103335"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1103334"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1103323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1103323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1103323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}