{"id":974784,"date":"2024-12-27T06:07:58","date_gmt":"2024-12-26T22:07:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974784.html"},"modified":"2024-12-27T06:08:00","modified_gmt":"2024-12-26T22:08:00","slug":"python%e5%a6%82%e4%bd%95%e5%8d%81byte%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974784.html","title":{"rendered":"python\u5982\u4f55\u5341byte\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200333\/2f2f4fee-6777-40b3-ab0b-047105ae8152.webp\" alt=\"python\u5982\u4f55\u5341byte\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8981\u5b9e\u73b0\u5bf9\u5341\u8fdb\u5236\u6570\u8fdb\u884c\u5b57\u8282\uff08byte\uff09\u7ea7\u522b\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528\u5185\u7f6e\u7684<code>int.to_bytes()<\/code>\u65b9\u6cd5\u3001<code>struct<\/code>\u6a21\u5757\u6216\u624b\u52a8\u5b9e\u73b0\u5b57\u8282\u8f6c\u6362\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u5982\u9700\u8981\u63a7\u5236\u5b57\u8282\u5e8f\uff08\u5927\u7aef\u6216\u5c0f\u7aef\uff09\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u6f14\u793a\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u6df1\u5165\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u64cd\u4f5c\u5341\u8fdb\u5236\u6570\u7684\u5b57\u8282\uff0c\u6211\u4eec\u9700\u8981\u5148\u4e86\u89e3\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5\u548c\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001INT.TO_BYTES()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>int.to_bytes()<\/code>\u65b9\u6cd5\u662fPython\u5185\u7f6e\u7684\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u7684\u51fd\u6570\u3002\u5b83\u5141\u8bb8\u4f60\u6307\u5b9a\u5b57\u8282\u6570\u548c\u5b57\u8282\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u65b9\u6cd5\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p><code>int.to_bytes()<\/code>\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">int.to_bytes(length, byteorder, *, signed=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>length<\/code>\uff1a\u6307\u5b9a\u8f6c\u6362\u540e\u7684\u5b57\u8282\u957f\u5ea6\u3002<\/li>\n<li><code>byteorder<\/code>\uff1a\u6307\u5b9a\u5b57\u8282\u5e8f\uff0c\u53ef\u4ee5\u662f<code>&#39;big&#39;<\/code>\u6216<code>&#39;little&#39;<\/code>\u3002<\/li>\n<li><code>signed<\/code>\uff1a\u6307\u5b9a\u662f\u5426\u4ee5\u6709\u7b26\u53f7\u6574\u6570\u5f62\u5f0f\u8868\u793a\uff0c\u9ed8\u8ba4\u662f\u65e0\u7b26\u53f7\u3002<\/li>\n<\/ul>\n<p><h4>1.2 \u4f7f\u7528\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6574\u6570<code>1024<\/code>\uff0c\u6211\u4eec\u60f3\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 1024<\/p>\n<p>byte_data = number.to_bytes(2, byteorder=&#39;big&#39;)<\/p>\n<p>print(byte_data)  # \u8f93\u51fa\uff1ab&#39;\\x04\\x00&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6574\u6570<code>1024<\/code>\u8f6c\u6362\u4e3a2\u4e2a\u5b57\u8282\u7684\u5927\u7aef\u5b57\u8282\u5e8f\u3002\u7ed3\u679c\u662f<code>b&#39;\\x04\\x00&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528STRUCT\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>struct<\/code>\u6a21\u5757\u7528\u4e8e\u5728Python\u4e2d\u5904\u7406C\u8bed\u8a00\u98ce\u683c\u7684\u7ed3\u6784\u4f53\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u8282\u548c\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u6a21\u5757\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p><code>struct<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>pack()<\/code>\u548c<code>unpack()<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u6570\u636e\u6253\u5305\u6210\u5b57\u8282\u6216\u8005\u4ece\u5b57\u8282\u89e3\u5305\u3002<\/p>\n<\/p>\n<ul>\n<li><code>pack(format, v1, v2, ...)<\/code>\uff1a\u5c06\u6570\u636e\u6253\u5305\u6210\u5b57\u8282\u3002<\/li>\n<li><code>unpack(format, buffer)<\/code>\uff1a\u5c06\u5b57\u8282\u89e3\u5305\u4e3a\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><h4>2.2 \u4f7f\u7528\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>struct.pack()<\/code>\u65b9\u6cd5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>number = 1024<\/p>\n<p>byte_data = struct.pack(&#39;&gt;H&#39;, number)<\/p>\n<p>print(byte_data)  # \u8f93\u51fa\uff1ab&#39;\\x04\\x00&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>&#39;&gt;H&#39;<\/code>\u8868\u793a\u5927\u7aef\u5b57\u8282\u5e8f\u548c\u65e0\u7b26\u53f7\u77ed\u6574\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u624b\u52a8\u5b9e\u73b0\u5b57\u8282\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u5b9e\u73b0\u5b57\u8282\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u8f6c\u6362\u903b\u8f91<\/h4>\n<\/p>\n<p><p>\u624b\u52a8\u8f6c\u6362\u9700\u8981\u4f7f\u7528\u4f4d\u8fd0\u7b97\uff0c\u5c06\u6574\u6570\u6309\u4f4d\u62c6\u5206\u4e3a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u7684\u7b80\u5355\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def int_to_bytes(n, length, byteorder=&#39;big&#39;):<\/p>\n<p>    byte_array = []<\/p>\n<p>    for i in range(length):<\/p>\n<p>        shift_amount = (length - 1 - i) * 8 if byteorder == &#39;big&#39; else i * 8<\/p>\n<p>        byte_array.append((n &gt;&gt; shift_amount) &amp; 0xFF)<\/p>\n<p>    return bytes(byte_array)<\/p>\n<p>number = 1024<\/p>\n<p>byte_data = int_to_bytes(number, 2)<\/p>\n<p>print(byte_data)  # \u8f93\u51fa\uff1ab&#39;\\x04\\x00&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u901a\u8fc7\u4f4d\u79fb\u548c\u63a9\u7801\u64cd\u4f5c\uff0c\u5c06\u6574\u6570\u62c6\u5206\u4e3a\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u8282\u5e8f\u7684\u7406\u89e3<\/h3>\n<\/p>\n<p><p>\u5b57\u8282\u5e8f\u662f\u5b57\u8282\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u987a\u5e8f\u7684\u7ea6\u5b9a\uff0c\u901a\u5e38\u5206\u4e3a\u5927\u7aef\u5e8f\uff08Big-endian\uff09\u548c\u5c0f\u7aef\u5e8f\uff08Little-endian\uff09\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5927\u7aef\u5e8f\u548c\u5c0f\u7aef\u5e8f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u5927\u7aef\u5e8f<\/strong>\uff1a\u9ad8\u4f4d\u5b57\u8282\u5b58\u50a8\u5728\u5185\u5b58\u7684\u4f4e\u5730\u5740\u3002<\/li>\n<li><strong>\u5c0f\u7aef\u5e8f<\/strong>\uff1a\u4f4e\u4f4d\u5b57\u8282\u5b58\u50a8\u5728\u5185\u5b58\u7684\u4f4e\u5730\u5740\u3002<\/li>\n<\/ul>\n<p><h4>4.2 \u5b57\u8282\u5e8f\u7684\u5f71\u54cd<\/h4>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u7cfb\u7edf\u53ef\u80fd\u4f7f\u7528\u4e0d\u540c\u7684\u5b57\u8282\u5e8f\uff0c\u8fd9\u4f1a\u5f71\u54cd\u6570\u636e\u5728\u4e0d\u540c\u7cfb\u7edf\u4e4b\u95f4\u7684\u4f20\u8f93\u548c\u5b58\u50a8\u3002\u56e0\u6b64\uff0c\u5728\u5904\u7406\u5b57\u8282\u6570\u636e\u65f6\uff0c\u6307\u5b9a\u5b57\u8282\u5e8f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f\u53ca\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u8282\u64cd\u4f5c\u5e38\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>5.1 \u7f51\u7edc\u901a\u4fe1<\/h4>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u5b57\u8282\u6d41\u7684\u5f62\u5f0f\u4f20\u8f93\uff0c\u56e0\u6b64\u9700\u8981\u8fdb\u884c\u5b57\u8282\u64cd\u4f5c\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u6587\u4ef6\u8bfb\u5199<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u4ee5\u4fbf\u4e8e\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>5.3 \u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u4e92\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\uff08\u5982C\u8bed\u8a00\uff09\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u65f6\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u5b57\u8282\u7ea7\u522b\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>5.4 \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u5b57\u8282\u5e8f<\/strong>\uff1a\u786e\u4fdd\u5728\u6570\u636e\u4f20\u8f93\u548c\u5b58\u50a8\u65f6\u4f7f\u7528\u4e00\u81f4\u7684\u5b57\u8282\u5e8f\u3002<\/li>\n<li><strong>\u5b57\u8282\u957f\u5ea6<\/strong>\uff1a\u786e\u4fdd\u8f6c\u6362\u7684\u5b57\u8282\u957f\u5ea6\u8db3\u591f\u5927\uff0c\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931\u3002<\/li>\n<li><strong>\u6709\u7b26\u53f7\u4e0e\u65e0\u7b26\u53f7<\/strong>\uff1a\u6839\u636e\u9700\u6c42\u9009\u62e9\u6709\u7b26\u53f7\u6216\u65e0\u7b26\u53f7\u6574\u6570\u8868\u793a\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u8fdb\u884c\u5341\u8fdb\u5236\u6570\u7684\u5b57\u8282\u64cd\u4f5c\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u65e0\u8bba\u662f\u4f7f\u7528<code>int.to_bytes()<\/code>\u3001<code>struct<\/code>\u6a21\u5757\uff0c\u8fd8\u662f\u624b\u52a8\u5b9e\u73b0\uff0c\u7406\u89e3\u8fd9\u4e9b\u5de5\u5177\u7684\u7528\u6cd5\u548c\u539f\u7406\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5341\u5b57\u8282\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5341\u5b57\u8282\u6570\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u8282\u5bf9\u8c61\u6765\u8868\u793a\u3002\u53ef\u4ee5\u4f7f\u7528<code>bytes<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5341\u4e2a\u5b57\u8282\u7684\u5b57\u8282\u5bf9\u8c61\uff0c\u4f8b\u5982\uff1a<code>my_bytes = bytes(10)<\/code>\u3002\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5341\u4e2a\u96f6\u5b57\u8282\u7684\u5b57\u8282\u5bf9\u8c61\u3002\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216\u5207\u7247\u64cd\u4f5c\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u8fd9\u4e9b\u5b57\u8282\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u8f6c\u6362\u5341\u5b57\u8282\u6570\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>int.from_bytes()<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u6307\u5b9a\u5b57\u8282\u987a\u5e8f\uff08\u5927\u7aef\u6216\u5c0f\u7aef\uff09\u3002\u4f8b\u5982\uff1a<code>int_value = int.from_bytes(my_bytes, &#39;big&#39;)<\/code>\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u5bf9\u8c61\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\uff0c\u524d\u63d0\u662f\u5b57\u8282\u5bf9\u8c61\u5305\u542b\u6709\u6548\u7684\u5b57\u7b26\u7f16\u7801\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u786e\u4fdd\u5341\u5b57\u8282\u6570\u7684\u6709\u6548\u6027\uff1f<\/strong><br \/>\u786e\u4fdd\u5341\u5b57\u8282\u6570\u7684\u6709\u6548\u6027\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u5b57\u8282\u5bf9\u8c61\u7684\u957f\u5ea6\u6765\u5b9e\u73b0\u3002\u4f7f\u7528<code>len(my_bytes)<\/code>\u6765\u786e\u8ba4\u5b57\u8282\u5bf9\u8c61\u786e\u5b9e\u5305\u542b\u5341\u4e2a\u5b57\u8282\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u7279\u5b9a\u7684\u6570\u503c\u8303\u56f4\uff0c\u53ef\u4ee5\u5728\u8f6c\u6362\u540e\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff0c\u786e\u4fdd\u5176\u503c\u5728\u9884\u671f\u7684\u8303\u56f4\u5185\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u5b9e\u73b0\u5bf9\u5341\u8fdb\u5236\u6570\u8fdb\u884c\u5b57\u8282\uff08byte\uff09\u7ea7\u522b\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528\u5185\u7f6e\u7684int.to_ [&hellip;]","protected":false},"author":3,"featured_media":974790,"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\/974784"}],"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=974784"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974784\/revisions"}],"predecessor-version":[{"id":974791,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974784\/revisions\/974791"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974790"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}