{"id":1144345,"date":"2025-01-08T22:58:51","date_gmt":"2025-01-08T14:58:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144345.html"},"modified":"2025-01-08T22:58:55","modified_gmt":"2025-01-08T14:58:55","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e4%b8%80%e4%b8%aa16%e4%bd%8d%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1144345.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181516\/3df80b81-ba7b-4c73-b8dd-58c213e195f0.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>struct<\/code>\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93\u5982<code>numpy<\/code>\u7b49\u5b9e\u73b0\u3002<strong>\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7c7b<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u4e14\u8bf4\u660e\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>struct<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684<code>struct<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5c06Python\u503c\u4e0eC\u8bed\u8a00\u7ed3\u6784\u4f53\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\u7684\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b9a\u4e49\u548c\u5904\u740616\u4f4d\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>struct<\/code>\u6a21\u5757\u7684<code>pack<\/code>\u548c<code>unpack<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u6253\u5305\u548c\u89e3\u5305\u6570\u636e\u3002\u5bf9\u4e8e16\u4f4d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5b57\u7b26<code>h<\/code>\uff08\u77ed\u6574\u578b\uff09\u6765\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>value = 0x1234<\/p>\n<p>packed_data = struct.pack(&#39;h&#39;, value)<\/p>\n<p>print(packed_data)<\/p>\n<h2><strong>\u89e3\u5305<\/strong><\/h2>\n<p>unpacked_value = struct.unpack(&#39;h&#39;, packed_data)[0]<\/p>\n<p>print(unpacked_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8be6\u7ec6\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p><strong>\u6253\u5305\u548c\u89e3\u5305<\/strong>\uff1a<code>struct.pack<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\uff0c\u800c<code>struct.unpack<\/code>\u65b9\u6cd5\u5219\u5c06\u5b57\u8282\u5e8f\u5217\u8f6c\u6362\u56de\u539f\u59cb\u6570\u636e\u3002\u683c\u5f0f\u5b57\u7b26<code>h<\/code>\u8868\u793a16\u4f4d\u77ed\u6574\u578b\uff0c<code>H<\/code>\u8868\u793a16\u4f4d\u65e0\u7b26\u53f7\u77ed\u6574\u578b\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u7f3a\u70b9<\/strong>\uff1a<code>struct<\/code>\u6a21\u5757\u975e\u5e38\u9002\u5408\u5904\u7406\u56fa\u5b9a\u683c\u5f0f\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f46\u5176\u64cd\u4f5c\u76f8\u5bf9\u5e95\u5c42\uff0c\u9700\u8981\u719f\u6089C\u8bed\u8a00\u7684\u7ed3\u6784\u4f53\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u7684\u652f\u6301\uff0c\u5e76\u5305\u542b\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u6765\u5b9a\u4e49\u548c\u64cd\u4f5c16\u4f4d\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>numpy<\/code>\u7684<code>int16<\/code>\u7c7b\u578b\u53ef\u4ee5\u7528\u6765\u5b9a\u4e4916\u4f4d\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>value = np.int16(0x1234)<\/p>\n<p>print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8be6\u7ec6\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p><strong>\u6570\u7ec4\u64cd\u4f5c<\/strong>\uff1a<code>numpy<\/code>\u4e0d\u4ec5\u53ef\u4ee5\u5b9a\u4e49\u5355\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u5305\u542b\u591a\u4e2a16\u4f4d\u6574\u6570\u7684\u6570\u7ec4\uff0c\u975e\u5e38\u9002\u5408\u8fdb\u884c\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u7f3a\u70b9<\/strong>\uff1a<code>numpy<\/code>\u5e93\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u77e9\u9635\u64cd\u4f5c\uff0c\u4f46\u5bf9\u4e8e\u7b80\u5355\u768416\u4f4d\u6570\u636e\u7c7b\u578b\u5904\u7406\uff0c\u53ef\u80fd\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7c7b<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7c7b\u6765\u5b9e\u73b016\u4f4d\u6570\u636e\u7c7b\u578b\u7684\u529f\u80fd\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5c01\u88c5\u548c\u7ba1\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9e\u73b016\u4f4d\u6570\u636e\u7c7b\u578b\u7684\u76f8\u5173\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Int16:<\/p>\n<p>    def __init__(self, value=0):<\/p>\n<p>        if not -32768 &lt;= value &lt;= 32767:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Value out of range for 16-bit integer&quot;)<\/p>\n<p>        self.value = value<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return f&quot;{self.value}&quot;<\/p>\n<p>    def to_bytes(self):<\/p>\n<p>        return self.value.to_bytes(2, byteorder=&#39;little&#39;, signed=True)<\/p>\n<p>    @staticmethod<\/p>\n<p>    def from_bytes(bytes_data):<\/p>\n<p>        return Int16(int.from_bytes(bytes_data, byteorder=&#39;little&#39;, signed=True))<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b<\/strong><\/h2>\n<p>value = Int16(0x1234)<\/p>\n<p>print(value)<\/p>\n<p>bytes_data = value.to_bytes()<\/p>\n<p>print(bytes_data)<\/p>\n<p>restored_value = Int16.from_bytes(bytes_data)<\/p>\n<p>print(restored_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8be6\u7ec6\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p><strong>\u5c01\u88c5\u548c\u6269\u5c55<\/strong>\uff1a\u901a\u8fc7\u81ea\u5b9a\u4e49\u7c7b\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5c01\u88c516\u4f4d\u6570\u636e\u7c7b\u578b\u7684\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u7f3a\u70b9<\/strong>\uff1a\u81ea\u5b9a\u4e49\u7c7b\u7684\u65b9\u5f0f\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u6269\u5c55\uff0c\u4f46\u9700\u8981\u7f16\u5199\u66f4\u591a\u7684\u4ee3\u7801\u6765\u5b9e\u73b0\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b\u6709\u591a\u79cd\u65b9\u6cd5\u3002<strong>\u4f7f\u7528<code>struct<\/code>\u6a21\u5757<\/strong>\u9002\u5408\u5904\u7406\u56fa\u5b9a\u683c\u5f0f\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c<strong>\u4f7f\u7528<code>numpy<\/code>\u5e93<\/strong>\u9002\u5408\u8fdb\u884c\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u548c\u77e9\u9635\u64cd\u4f5c\uff0c\u800c<strong>\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7c7b<\/strong>\u5219\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u5c01\u88c5\u548c\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u5e0c\u671b\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u768416\u4f4d\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u6765\u5b9a\u4e49\u548c\u5904\u740616\u4f4d\u7684\u6570\u636e\u7c7b\u578b\u3002\u901a\u8fc7<code>struct.pack<\/code>\u548c<code>struct.unpack<\/code>\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u89e3\u679016\u4f4d\u6574\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5b57\u7b26<code>&#39;h&#39;<\/code>\uff08\u8868\u793a\u6709\u7b26\u53f716\u4f4d\u6574\u6570\uff09\u6216<code>&#39;H&#39;<\/code>\uff08\u8868\u793a\u65e0\u7b26\u53f716\u4f4d\u6574\u6570\uff09\u8fdb\u884c\u6253\u5305\u548c\u89e3\u5305\u3002<\/p>\n<p><strong>Python\u652f\u6301\u768416\u4f4d\u6570\u636e\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u89c1\u768416\u4f4d\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6709\u7b26\u53f7\u6574\u6570\uff08<code>int16<\/code>\uff09\u548c\u65e0\u7b26\u53f7\u6574\u6570\uff08<code>uint16<\/code>\uff09\u3002\u4f7f\u7528<code>numpy<\/code>\u5e93\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u8fd9\u4e24\u79cd\u6570\u636e\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u65b9\u4fbf\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u5b9a\u4e4916\u4f4d\u6574\u6570\u6570\u7ec4\uff1f<\/strong><br \/>\u901a\u8fc7<code>numpy<\/code>\u5e93\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>numpy.int16<\/code>\u548c<code>numpy.uint16<\/code>\u6765\u5b9a\u4e4916\u4f4d\u6574\u6570\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b16\u4f4d\u6709\u7b26\u53f7\u6574\u6570\u7684\u6570\u7ec4\uff0c\u5982\u4e0b\u6240\u793a\uff1a<code>import numpy as np; arr = np.array([1, 2, 3], dtype=np.int16)<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u80fd\u5229\u7528NumPy\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b \u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a16\u4f4d\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684stru [&hellip;]","protected":false},"author":3,"featured_media":1144354,"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\/1144345"}],"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=1144345"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144345\/revisions"}],"predecessor-version":[{"id":1144356,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144345\/revisions\/1144356"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144354"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}