{"id":205302,"date":"2025-05-29T08:23:06","date_gmt":"2025-05-29T00:23:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205302\/"},"modified":"2025-05-29T08:23:06","modified_gmt":"2025-05-29T00:23:06","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%86%99%e5%85%a5-python-%e4%ba%8c%e8%bf%9b%e5%88%b6%e6%96%87%e4%bb%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205302\/","title":{"rendered":"\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5199\u5165 Python \u4e8c\u8fdb\u5236\u6587\u4ef6\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5199\u5165 Python \u4e8c\u8fdb\u5236\u6587\u4ef6\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/173278391567482f2b5901e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u7528 python \u5c06\u5b57\u7b26\u4e32\u5199\u5165\u4e8c\u8fdb\u5236\u5e8f\u5217<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u4f7f\u7528\u4e8c\u8fdb\u5236\u6587\u4ef6\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3002\u8981\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u9700\u8981\u4f7f\u7528 struct \u6a21\u5757\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\u3002<\/p>\n<p><strong>\u4f7f\u7528 struct.pack() \u7f16\u7801\u5b57\u7b26\u4e32<\/strong><\/p>\n<p>struct.pack() \u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5e8f\u5217\u3002\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5e8f\u5217\uff0c\u9700\u8981\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u683c\u5f0f\u5b57\u7b26\u4e32\u4e3a &#8216;b&#8217;\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32 &#8220;abcd&#8221; \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1a<\/p>\n<pre>import struct\n\nstring = \"abcd\"\nbinary_sequence = struct.pack('4b', *string.encode())<\/pre>\n<p>*string.encode() \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\uff0c\u4ee5\u4fbf struct.pack() \u53ef\u4ee5\u5c06\u5176\u6253\u5305\u4e3a\u4e8c\u8fdb\u5236\u5e8f\u5217\u3002<\/p>\n<p><strong>\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 open() \u51fd\u6570\u4ee5\u4e8c\u8fdb\u5236\u5199\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u3002\u7136\u540e\uff0c\u4f7f\u7528 write() \u65b9\u6cd5\u5c06\u4e8c\u8fdb\u5236\u5e8f\u5217\u5199\u5165\u6587\u4ef6\uff1a<\/p>\n<pre>with open(\"binary_file.bin\", \"wb\") as f:\n    f.write(binary_sequence)<\/pre>\n<p>\u4f7f\u7528 wb \u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u8868\u793a\u4ee5\u4e8c\u8fdb\u5236\u5199\u6a21\u5f0f\u6253\u5f00\u3002<\/p>\n<p><strong>\u9a8c\u8bc1\u5199\u5165\u7ed3\u679c<\/strong><\/p>\n<p>\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u7f16\u8f91\u5668\u6253\u5f00\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u53ef\u4ee5\u67e5\u770b\u5199\u5165\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u662f\u5426\u6b63\u786e\u3002\u5341\u516d\u8fdb\u5236\u8868\u793a\u5e94\u8be5\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n<pre>61 62 63 64<\/pre>\n<p>\u8fd9\u4ee3\u8868\u7740\u5b57\u7b26\u4e32 &#8220;abcd&#8221; \u7684\u5341\u516d\u8fdb\u5236\u8868\u793a\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5199\u5165 Python \u4e8c\u8fdb\u5236\u6587\u4ef6\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5199\u5165 Python &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205302","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205302"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205302\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}