{"id":215,"date":"2017-04-06T05:02:58","date_gmt":"2017-04-06T05:02:58","guid":{"rendered":""},"modified":"2017-04-06T05:02:58","modified_gmt":"2017-04-06T05:02:58","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=215","title":{"rendered":"[\u8f6c]ctypes+struct\u5b9e\u73b0\u7c7bc\u7684\u7ed3\u6784\u5316\u6570\u636e\u4e32\u884c\u5904\u7406"},"content":{"rendered":"<p>\n\t<span style=\"font-size:14px;\"><a href=\"http:\/\/blog.csdn.net\/machael_sonic\/article\/details\/50266499\" target=\"_blank\">http:\/\/blog.csdn.net\/machael_sonic\/article\/details\/50266499<\/a><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><br \/>\n<\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\">1. \u7528C\/C++\u5b9e\u73b0\u7684\u7ed3\u6784\u5316\u6570\u636e\u5904\u7406<\/span>\n<\/p>\n<p>\n<span style=\"font-size:14px;\">\u5728\u6d89\u53ca\u5230\u6bd4\u8f83\u5e95\u5c42\u7684\u901a\u4fe1\u534f\u8bae\u5f00\u53d1\u8fc7\u7a0b\u4e2d, \u5f80\u5f80\u9700\u8981\u5f00\u53d1\u8bed\u8a00\u80fd\u591f\u6709\u6548\u7684\u8868\u8fbe\u548c\u5904\u7406\u6240\u5b9a\u4e49\u7684\u901a\u4fe1\u534f\u8bae\u7684\u6570\u636e\u7ed3\u6784. \u5728\u8fd9\u65b9\u9762\u662fC\/C++\u8bed\u8a00\u662f\u5177\u6709\u5929\u7136\u4f18\u52bf\u7684: \u901a\u8fc7struct, union, \u548cbit-fields, C\/C++\u80fd\u591f\u4ee5\u4e00\u79cd\u6700\u6709\u6548\u7387\u4e5f\u6700\u81ea\u7136\u7684\u65b9\u5f0f\u5904\u7406\u6b64\u7c7b\u95ee\u9898.<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u4e3e\u4f8b\u8bf4\u660e\u4e00\u4e0b, \u4e0b\u56fe\u662f\u667a\u80fd\u7535\u7f51\u7528\u4e8e\u8fdc\u7a0b\u81ea\u52a8\u6284\u8868\u7684\u901a\u4fe1\u534f\u8bae\u7684\u4e00\u90e8\u5206&nbsp;<\/span><br \/>\n<img decoding=\"async\" src=\"http:\/\/img.blog.csdn.net\/20151211103048615\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" title=\"\" style=\"border-width:initial;border-style:none;box-sizing:border-box;max-width:602px;height:auto;color:#555555;font-family:&quot;font-size:14px;white-space:normal;background-color:#FFFFFF;\" \/><span style=\"color:#555555;font-family:&quot;font-size:14px;white-space:normal;background-color:#FFFFFF;\">&nbsp;<\/span><br style=\"box-sizing:border-box;color:#555555;font-family:&quot;font-size:14px;white-space:normal;background-color:#FFFFFF;\" \/><br \/>\n<img decoding=\"async\" src=\"http:\/\/img.blog.csdn.net\/20151211103101439\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" title=\"\" style=\"border-width:initial;border-style:none;box-sizing:border-box;max-width:602px;height:auto;color:#555555;font-family:&quot;font-size:14px;white-space:normal;background-color:#FFFFFF;\" \/><\/p>\n<p><span style=\"font-size:14px;\">\u7528C\u53ef\u4ee5\u63cf\u8ff0\u5982\u4e0b:<\/span><\/p>\n<p><span style=\"font-size:14px;\">&nbsp; struct<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; {<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char &nbsp;uRouter:1; &nbsp;\/\/\u8def\u7531\u6807\u8bc6<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char &nbsp; uSubNode:1;\/\/\u9644\u5c5e\u8282\u70b9\u6807\u8bc6<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char &nbsp; uCM:1; &nbsp; &nbsp; \/\/\u901a\u4fe1\u6a21\u5757\u6807\u8bc6<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char &nbsp; uCD:1; &nbsp; &nbsp; \/\/\u51b2\u7a81\u68c0\u6d4b<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char &nbsp; uLevel:4; &nbsp;\/\/\u4e2d\u7ee7\u7ea7\u522b<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char &nbsp; uChannel:4;\/\/\u4fe1\u9053\u6807\u8bc6<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char &nbsp; uErrBate:4;\/\/\u7ea0\u9519\u7f16\u7801\u6807\u8bc6<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char &nbsp; uResBytes; \/\/\u9884\u8ba1\u5e94\u7b54\u5b57\u8282\u6570<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned short &nbsp;uSpeed:15; \/\/\u901a\u4fe1\u6ce2\u7279\u7387,BIN\u683c\u5f0f<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned short &nbsp;uUnit:1; &nbsp; \/\/0:bps;1:kbps<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char &nbsp;uReserve;<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; } Req;<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u8fd9\u6837\u4e0d\u4ec5\u6e05\u695a\u7684\u63cf\u8ff0\u4e86\u5b8c\u5168\u7b26\u5408\u901a\u4fe1\u534f\u8bae\u8981\u6c42\u7684\u62a5\u6587\u6570\u636e\u7ed3\u6784, \u800c\u4e14\u8fd8\u6709\u81f3\u5c11\u4ee5\u4e0b\u4e24\u4e2a\u4f18\u70b9:&nbsp;<\/span><br \/>\n<span style=\"font-size:14px;\">1. \u5bf9\u7ed3\u6784\u4e2d\u7684\u4efb\u610f\u53d8\u91cf\u53d6\u5740\u8d4b\u503c\u53d6\u503c\u6781\u5176\u65b9\u4fbf, \u5982<\/span><\/p>\n<p><span style=\"font-size:14px;\">&nbsp; &nbsp; struct Req r;<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; r.uCD = 0;<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; r.uChannel = 0x0F;<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u5e76\u4e0d\u5fc5\u8d39\u5fc3\u7684\u8ba1\u7b97\u504f\u79fb\u91cf. \u800c\u4e14\u5982\u679c\u4ee5\u540e\u901a\u4fe1\u534f\u8bae\u5347\u7ea7\u4e86, \u53ea\u9700\u8981\u5c06\u6570\u636e\u7ed3\u6784\u5b9a\u4e49\u66f4\u6539\u5373\u53ef, \u5176\u4f59\u4ee3\u7801\u5b8c\u5168\u4e0d\u7528\u53d8\u52a8.&nbsp;<\/span><br \/>\n<span style=\"font-size:14px;\">2. \u66f4\u91cd\u8981\u7684\u662f, \u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u5929\u7136\u7684\u5c31\u662f\u6309\u7167\u901a\u4fe1\u534f\u8bae\u7684\u4e32\u884c\u7ed3\u6784\u6392\u5217\u7684(\u5047\u8bbe\u5927\u7aef\u5c0f\u7aef\u95ee\u9898\u5df2\u8bbe\u7f6e\u6b63\u786e), \u53ea\u9700\u8981<\/span><\/p>\n<p><span style=\"font-size:14px;\">&nbsp; &nbsp; struct Req r;<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &#8230;<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; send((unsigned char *)&amp;r, sizeof(r));<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u5c31\u53ef\u4ee5\u4ee5\u901a\u4fe1\u534f\u8bae\u5b8c\u5168\u4e00\u81f4\u7684\u683c\u5f0f\u5c06\u6570\u636e\u8f6c\u6362\u6210\u5b57\u8282\u6d41\u53d1\u9001\u51fa\u53bb\u4e86. \u800c\u63a5\u6536\u89e3\u6790\u4e5f\u540c\u6837\u65b9\u4fbf:<\/span><\/p>\n<p><span style=\"font-size:14px;\">&nbsp; &nbsp; struct Req rs;<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; unsigned char rcv_buffer[100];<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &#8230;<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; rcv(rcv_buffer sizeof(Req));<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; memcpy((unsigned char *)&amp;rs, rcv_buffer, sizeof(r));<\/span><\/p>\n<p><span style=\"font-size:14px;\">2. \u7528Python\u5b9e\u73b0\u7684\u7ed3\u6784\u5316\u6570\u636e\u5904\u7406<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u73b0\u5728\u95ee\u9898\u6765\u4e86: \u5982\u679c\u7528Python, \u8fd8\u80fd\u591f\u540c\u6837\u65b9\u4fbf\u7684\u5b9e\u73b0\u4e0a\u8ff0\u7684\u7ed3\u6784\u5316\u6570\u636e\u5904\u7406\u5417? \u4e5f\u5c31\u662f\u9700\u8981\u5b9e\u73b0\u4ee5\u4e0b\u529f\u80fd:<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u80fd\u591f\u4ee5\u53d8\u91cf\u540d\u8bbf\u95ee\u6570\u636e\u6bb5, \u4e0d\u9700\u8981\u624b\u52a8\u8ba1\u7b97\u504f\u79fb\u91cf<\/span><br \/>\n<span style=\"font-size:14px;\">\u80fd\u591f\u5904\u7406bit\u7ea7\u7684\u6570\u636e\u6bb5<\/span><br \/>\n<span style=\"font-size:14px;\">\u80fd\u591f\u65b9\u4fbf\u7684\u5f62\u6210\u4e32\u884c\u5316\u901a\u4fe1\u5b57\u8282\u6d41, \u4e5f\u80fd\u65b9\u4fbf\u7684\u4ece\u63a5\u6536\u7684\u5b57\u8282\u6d41\u4e2d\u89e3\u6790\u6570\u636e;<\/span><br \/>\n<span style=\"font-size:14px;\">\u6709\u4eba\u53ef\u80fd\u89c9\u5f97\u8fd9\u4e0d\u662f\u95ee\u9898: \u7528python\u7684\u5b57\u5178\u4e0d\u662f\u4e5f\u80fd\u5b9e\u73b0\u5417? \u4ed4\u7ec6\u60f3\u4e00\u60f3, \u5b57\u5178\u53ea\u80fd\u591f\u63d0\u4f9b\u7b2c\u4e00\u79cd\u9700\u6c42, \u5373\u4ee5\u53d8\u91cf\u540d\u8bbf\u95ee\u6570\u636e\u6bb5. \u4f46python\u56e0\u4e3a\u662f\u9ad8\u7ea7\u8bed\u8a00, \u6574\u6570\u53ea\u63d0\u4f9bint\u4e00\u79cd\u6570\u636e\u7ed3\u6784, \u800c\u534f\u8bae\u4e2d\u5f88\u591a\u65f6\u5019\u6570\u636e\u6bb5\u662fbit\u7ea7\u7684, \u6216\u5355\u5b57\u8282, \u4e24\u5b57\u8282, \u4e09\u5b57\u8282\u7684. \u53ea\u7528python\u539f\u751f\u7684\u6570\u636e\u7ed3\u6784\u662f\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95eebit\u7ea7\u7684\u6570\u636e\u6bb5\u7684, \u751a\u81f3\u8fde\u6570\u636e\u4f53\u6700\u540e\u5230\u5e95\u5360\u4e86\u51e0\u5b57\u8282, \u90fd\u4e0d\u80fd\u65b9\u4fbf\u7684\u7edf\u8ba1.<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898, \u672c\u8d28\u8fd8\u662f\u8981\u9000\u56de\u5230C\u8bed\u8a00\u7684\u7ea7\u522b\u6765. \u597d\u5728python\u63d0\u4f9b\u4e86ctypes\u8fd9\u4e2a\u5e93, \u80fd\u591f\u8ba9\u6211\u4eec\u5728python\u4e2d\u5b9e\u73b0\u7c7b\u4f3cC\u8bed\u8a00\u7684\u529f\u80fd.<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; from ctypes import *<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; class Req(Structure):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;uRouter&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uSubNode&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uCM&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uCD&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uLevel&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uChannel&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uErrBate&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uResBytes&#8217;,c_ubyte),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uSpeed&#8217;,c_ushort,15),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uUnit&#8217;,c_ushort,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uReserve&#8217;,c_ubyte)]<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r=Req()<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; sizeof(r)<\/span><br \/>\n<span style=\"font-size:14px;\">8<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r.uUnit=1<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; print r.uUnit<\/span><br \/>\n<span style=\"font-size:14px;\">1<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r.uUnit=2<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; print r.uUnit<\/span><\/p>\n<p><span style=\"font-size:14px;\">ctypes\u5e93\u7684\u6700\u4e3b\u8981\u4f5c\u7528\u5176\u5b9e\u662f\u7528\u4e8epython\u7a0b\u5e8f\u8c03\u7528c\u7f16\u8bd1\u5668\u751f\u6210\u7684\u5e93\u548cdll, \u4f46\u6211\u4eec\u8fd9\u91cc\u53ea\u7528\u5230\u6570\u636e\u7ed3\u6784\u8fd9\u4e00\u5757.<\/span><\/p>\n<p><span style=\"font-size:14px;\">ctypes\u5728\u4f7f\u7528\u65f6\u6709\u4ee5\u4e0b\u6ce8\u610f\u4e8b\u9879:<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u81ea\u5b9a\u4e49\u7684\u7ed3\u6784\u4f53\u7c7b\u5fc5\u987b\u7ee7\u627fStructure\u6216Union\u7c7b;<\/span><br \/>\n<span style=\"font-size:14px;\">\u81ea\u5b9a\u4e49\u7684\u7ed3\u6784\u4f53\u7c7b\u4e2d\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3afields\u7684\u5217\u8868\u53d8\u91cf, \u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u4e00\u4e2atuple, \u5b9a\u4e49\u4e86\u7ed3\u6784\u4f53\u6bcf\u4e2a\u6570\u636e\u5355\u5143\u4fe1\u606f, \u683c\u5f0f\u662f(\u2018\u53d8\u91cf\u540d\u5b57\u7b26\u4e32\u2019, \u53d8\u91cf\u6570\u636e\u7c7b\u578b [, \u6bd4\u7279\u6570])<\/span><br \/>\n<span style=\"font-size:14px;\">\u5b9a\u4e49\u4e86class\u540e, \u53ef\u4ee5\u7528sizeof(\u7c7b\u540d)\u67e5\u770b\u6570\u636e\u4f53\u5b57\u8282\u6570, \u548cc\u8bed\u8a00\u4e00\u6837. \u7136\u540e\u7528\u5b9e\u4f8b\u540d.\u6210\u5458\u540d\u8fdb\u884c\u76f8\u5e94\u6570\u636e\u5355\u5143\u7684\u8bbf\u95ee, \u5982\u679c\u7ee7\u627f\u540e\u5b9a\u4e49\u4e86init()\u65b9\u6cd5, \u8fd8\u53ef\u4ee5\u8fdb\u884c\u7c7b\u7684\u521d\u59cb\u5316\u64cd\u4f5c<\/span><br \/>\n<span style=\"font-size:14px;\">3. \u4e32\u884c\u6570\u636e\u6d41\u5904\u7406<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u6709\u4e86\u7ed3\u6784\u4f53, \u4e0a\u9762\u7684\u4e09\u6761\u8981\u6c42\u6ee1\u8db3\u4e86\u4fe9\u4e2a, \u5173\u4e8e\u7b2c\u4e09\u4e2a\u8981\u6c42, ctypes\u867d\u7136\u63d0\u4f9b\u4e86cast()\u65b9\u6cd5, \u4f46\u7ecf\u8fc7\u6211\u7814\u7a76, \u53d1\u73b0cast\u5176\u5b9e\u53ea\u80fd\u5b9e\u73b0\u7b80\u5355\u7684\u6570\u7ec4\u7b49\u7ed3\u6784\u7684\u6570\u636e\u7c7b\u578b\u6307\u9488\u8f6c\u6362, \u4f46\u65e0\u6cd5\u50cfc\u90a3\u6837\u5c06\u7ed3\u6784\u4f53\u5bf9\u8c61\u5730\u5740\u8f6c\u6362\u6210\u5b57\u8282\u5730\u5740\u7684. \u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u9700\u8981python\u7684\u53e6\u4e00\u4e2a\u5e93:struct<\/span><\/p>\n<p><span style=\"font-size:14px;\">struct\u662f\u4e13\u95e8\u7528\u4e8e\u7ed3\u6784\u4f53\u4e0e\u6570\u636e\u6d41\u8f6c\u6362\u7684\u5e93, \u6211\u4eec\u7528\u5230\u7684\u4e3b\u8981\u65b9\u6cd5\u662fpack()\u548cunpack(). pack()\u7684\u4f7f\u7528\u8bf4\u660e\u5982\u4e0b:<\/span><\/p>\n<p><span style=\"font-size:14px;\">struct.pack(fmt, v1, v2, \u2026)&nbsp;<\/span><br \/>\n<span style=\"font-size:14px;\">Return a string containing the values v1, v2, \u2026 packed according to the given format. The arguments must match the values required by the format exactly.<\/span><br \/>\n<span style=\"font-size:14px;\">\u4e3e\u4e2a\u4f8b\u5b50:<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; pack(&#8216;BHB&#8217;,1,2,3)<\/span><br \/>\n<span style=\"font-size:14px;\">&#8216;\\x01\\x00\\x02\\x00\\x03&#8217;<\/span><\/p>\n<p><span style=\"font-size:14px;\">pack()\u7684\u7528\u6cd5\u548cformat()\u5f88\u50cf, \u7b2c\u4e00\u4e2a\u53c2\u6570\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u6307\u660e\u4e86\u8981\u8f6c\u6362\u7684\u683c\u5f0f, \u4f8b\u5982\u2019B\u2019\u8868\u793a8\u4f4d\u65e0\u7b26\u53f7\u6574\u6570, \u2018H\u2019\u8868\u793a16\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u7b49\u7b49, \u5177\u4f53\u8be6\u89c1python\u5e2e\u52a9\u91cc\u5173\u4e8estruct\u5e93\u7684\u8bf4\u660e. \u8fd9\u91cc\u7684\u2019BHB\u2019\u5c31\u7b49\u4e8e\u6307\u660e\u4e86, \u5c06\u540e\u9762\u7684\u4e09\u4e2a\u6570\u8f6c\u6210\u5b57\u8282\u6d41, \u7b2c\u4e00\u4e2a\u6570\u4ee58\u4f4d\u65e0\u7b26\u53f7\u6570\u8868\u793a, \u7b2c\u4e8c\u4e2a\u4ee516\u4f4d\u65e0\u7b26\u53f7\u6570\u8868\u793a, \u7b2c\u4e09\u4e2a\u4ee58\u4f4d\u65e0\u7b26\u53f7\u6570\u8868\u793a.<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u7b49\u7b49! \u54ea\u91cc\u4e0d\u5bf9\u554a? \u4e24\u4e2a8\u4f4d\u65e0\u7b26\u53f7\u6570, \u4e00\u4e2a16\u4f4d\u65e0\u7b26\u53f7\u6570, \u52a0\u8d77\u6765\u5e94\u8be54\u4e2a\u5b57\u8282\u624d\u5bf9. \u53ef\u662f\u6211\u4eec\u770b\u8f6c\u6362\u7ed3\u679c\u2019\\x01\\x00\\x02\\x00\\x03\u2019\u4e00\u5171\u662f\u4e94\u4e2a\u5b57\u8282, \u6700\u540e\u4e00\u4e2a3\u4e5f\u88ab\u5f5316\u65e0\u7b26\u53f7\u6570\u5904\u7406\u4e86, \u96be\u9053\u662fbug\u4e86?<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u8fd9\u4e2a\u95ee\u9898\u5176\u5b9e\u5728\u5e2e\u52a9\u6587\u6863\u91cc\u4e5f\u8bf4\u7684\u5f88\u6e05\u695a\u4e86, \u8fd9\u662f\u6240\u8c13machine\u2019s native format\u548cstandard format\u7684\u533a\u522b. \u7b80\u800c\u8a00\u4e4b\u5c31\u662f, \u5bf9\u4e8e\u6709\u4e9bC\u7f16\u8bd1\u5668, \u5982\u679c\u6ca1\u6709\u505a\u7279\u6b8a\u7f16\u8bd1\u7ea6\u675f, \u51fa\u4e8e\u5904\u7406\u5b57\u5bbd\u7684\u8003\u8651, \u5bf9\u7c7b\u4f3cunsigned char\u8fd9\u6837\u7684\u6570\u636e, \u5e76\u975e\u771f\u7684\u75281\u5b57\u8282\u8868\u793a, \u800c\u662f\u7528\u5904\u7406\u65f6\u6700\u9002\u5408cpu\u5bc4\u5b58\u5668\u7684\u957f\u5ea6\u8868\u793a, \u6bd4\u5982\u8ddf\u5728\u4e00\u4e2a\u65e0\u7b26\u53f716\u4f4d\u6570\u540e\u9762\u7684\u4e00\u4e2a\u65e0\u7b26\u53f78\u4f4d\u6570, \u5c31\u540c\u6837\u752816\u4f4d\u4f4d\u5bbd\u8868\u793a. \u8fd9\u6837\u5c3d\u7ba1\u6d6a\u8d39\u4e86\u5185\u5b58, \u4f46\u5728\u5bfb\u5740\u8d4b\u503c\u7b49\u5904\u7406\u8d77\u6765\u66f4\u6709\u6548\u7387\u2026 \u603b\u800c\u8a00\u4e4b, \u5982\u679c\u4e00\u5b9a\u8981\u6c42\u4e25\u683c\u76848\u4f4d\u548c16\u4f4d, \u5c31\u9700\u8981\u4f7f\u7528standard format, \u5c31\u662f\u5728\u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u52a0\u4ee5\u9650\u5b9a, \u5982:<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; pack(&#8216;&gt;BhB&#8217;,1,2,3)<\/span><br \/>\n<span style=\"font-size:14px;\">&#8216;\\x01\\x00\\x02\\x03&#8217;<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u8fd9\u91cc\u7684&gt;\u8868\u793a: \u5b57\u8282\u6d41\u8f6c\u6362\u4f7f\u7528standard format, \u800c\u4e14\u4f7f\u7528\u5927\u7aef\u6a21\u5f0f.<\/span><\/p>\n<p><span style=\"font-size:14px;\">4. \u7ed3\u6784\u4f53\u7684\u5b57\u8282\u6d41\u8f6c\u6362<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u6709\u4e86pack()\u8fd9\u4e2a\u5de5\u5177, \u518d\u56de\u5230\u524d\u9762\u7684\u7ed3\u6784\u4f53\u5b57\u8282\u6d41\u8f6c\u6362\u4e0a\u2026 \u53d1\u73b0\u8fd8\u662f\u6709\u95ee\u9898\u554a, \u56e0\u4e3apack()\u53ef\u4ee5\u5b9e\u73b0\u5355\u5b57\u8282, \u53cc\u5b57\u8282, \u5374\u6ca1\u6cd5\u5bf9bit field\u8fd9\u79cd\u4e1c\u897f\u64cd\u4f5c. \u53c8\u8be5\u600e\u4e48\u89e3\u51b3\u5462.<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u5176\u5b9e\u8fd9\u4e2a\u95ee\u9898, \u6211\u4e5f\u6ca1\u627e\u5230\u597d\u7684\u89e3\u51b3\u529e\u6cd5, \u6bd5\u7adfpack()\u9700\u8981\u6211\u4eec\u624b\u5de5\u4e00\u4e2a\u4e2a\u6307\u5b9a\u53d8\u91cf, \u5b9a\u4e49\u987a\u5e8f\u548c\u5b57\u8282\u957f\u5ea6. \u8fd9\u91cc\u6211\u63d0\u4f9b\u4e00\u79cd\u89e3\u51b3\u65b9\u6848, \u90a3\u5c31\u662f\u501f\u7528Union.<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u4ecd\u4ee5\u524d\u9762\u7684\u7ed3\u6784\u4f53\u4e3a\u4f8b, \u6362\u4e00\u79cd\u5199\u6cd5:<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; class Flag_Struct(Structure):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;uRouter&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uSubNode&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uCM&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uCD&#8217;,c_ubyte,1),<\/span><\/p>\n<p>\n\t<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uLevel&#8217;,c_ubyte,4)]<\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><br \/>\n<\/span>\n<\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; class Flag_Union(Union):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;whole&#8217;,c_ubyte),<\/span><\/p>\n<p>\n\t<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;flag_struct&#8217;,Flag_Struct)]<\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><br \/>\n<\/span>\n<\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; class Channel_Struct(Structure):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;uChannel&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uErrBate&#8217;,c_ubyte,4)]<\/span><\/p>\n<p>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; class Channel_Union(Union):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;whole&#8217;,c_ubyte),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;channel_struct&#8217;,Channel_Struct)]<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; class Speed_Struct(Structure):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;uSpeed&#8217;,c_ushort,15),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uUnit&#8217;,c_ushort,1)]<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; class Speed_Union(Union):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;whole&#8217;,c_ushort),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;speed_struct&#8217;,Speed_Struct)]<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; class Req(Structure):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _pack_=1<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;flag&#8217;,Flag_Union),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;channel&#8217;,Channel_Union),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uResBytes&#8217;,c_ubyte),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;speed&#8217;,Speed_Union),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uReserve&#8217;,c_ubyte)]<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u7b80\u800c\u8a00\u4e4b, \u5c31\u662f\u6240\u6709\u6d89\u53cabit-field\u7684\u5b57\u6bb5\u90fd\u7528\u4e00\u4e2aunion\u548c\u5b50struct\u6765\u8868\u793a. (\u5176\u4e2dpack\u662f\u4e3a\u4e861\u5b57\u8282\u5bf9\u9f50, \u539f\u56e0\u4e0e\u4e0a\u4e00\u8282\u4ecb\u7ecd\u8fc7\u7684native format\u548cstandard format\u7c7b\u4f3c). \u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u6298\u4e2d\u6bd4\u7279\u5b57\u6bb5\u8bbf\u95ee\u4e0e\u6574\u5b57\u8282\u7684\u8f6c\u5316\u5904\u7406, \u4f8b\u5982:<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; r=Req()<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r.speed.speed_struct.uUnit=1<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r.flag.flag_struct.uLevel=0xf<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; ack(&#8216;&gt;BBBHB&#8217;,r.flag.whole,r.channel.whole,r.uResBytes,r.speed.whole,r.uReserve)<\/span><br \/>\n<span style=\"font-size:14px;\">&#8216;\\xf0\\x00\\x00\\x80\\x00\\x00&#8217;<\/span><\/p>\n<p><span style=\"font-size:14px;\">5. <strong>\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u5b57\u8282\u6d41\u8f6c\u5316\u65b9\u6cd5<\/strong><\/span><\/p>\n<p><span style=\"font-size:14px;\">\u540e\u6765\u901a\u8fc7\u4ed4\u7ec6\u67e5\u770b\u6587\u6863, \u53d1\u73b0\u5176\u5b9ectypes\u91cc\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u5355\u7684\u5b57\u8282\u6d41\u8f6c\u5316\u65b9\u6cd5:<\/span><br \/>\n<span style=\"font-size:14px;\">string_at(addressof(r),sizeof(r))<\/span><\/p>\n<p><span style=\"font-size:14px;\">addressof()\u548cstring_at\u90fd\u662fctypes\u91cc\u63d0\u4f9b\u7684\u65b9\u6cd5. \u8fd9\u662f\u6700\u63a5\u8fd1\u4e8e\u539f\u751fc\u7684\u5904\u7406\u65b9\u6cd5, \u8fd9\u6837\u8fdeunion\u90fd\u4e0d\u7528\u5b9a\u4e49\u4e86<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; class Req(Structure):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _pack_=1<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;uRouter&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uSubNode&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uCM&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uCD&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uLevel&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uChannel&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uErrBate&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uResBytes&#8217;,c_ubyte),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uSpeed&#8217;,c_ushort,15),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uUnit&#8217;,c_ushort,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uReserve&#8217;,c_ubyte)]<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; sizeof(Req)<\/span><br \/>\n<span style=\"font-size:14px;\">6<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r=Req()<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r.uUnit=1<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r.uCM=1<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; string_at(addressof(r),sizeof(r))<\/span><br \/>\n<span style=\"font-size:14px;\">&#8216;\\x04\\x00\\x00\\x00\\x80\\x00&#8217;<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u5982\u679c\u9700\u8981\u5927\u7aef\u7684\u6570\u636e\u7ed3\u6784, \u8d85\u7c7b\u9700\u8981\u9009\u62e9BigEndianStructure, \u6b64\u65f6bit-field\u7684\u5b9a\u4e49\u4e5f\u662f\u4ece\u9ad8\u5230\u4f4e\u7684, \u9700\u8981\u91cd\u65b0\u8c03\u6574\u5b9a\u4e49\u7684\u987a\u5e8f, \u5982\u4e0b:<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; class Req(BigEndianStructure):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _pack_=1<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; _fields_=[(&#8216;uLevel&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uCD&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uCM&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uSubNode&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uRouter&#8217;,c_ubyte,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uErrBate&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uChannel&#8217;,c_ubyte,4),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uResBytes&#8217;,c_ubyte),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uUnit&#8217;,c_ushort,1),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uSpeed&#8217;,c_ushort,15),<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#8216;uReserve&#8217;,c_ubyte)]<\/span><\/p>\n<p><span style=\"font-size:14px;\">&gt;&gt;&gt; r=Req()<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r.uLevel=0xf<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; r.uUnit=1<\/span><br \/>\n<span style=\"font-size:14px;\">&gt;&gt;&gt; string_at(addressof(r),sizeof(r))<\/span><br \/>\n<span style=\"font-size:14px;\">&#8216;\\xf0\\x00\\x00\\x80\\x00\\x00&#8217;<\/span><\/p>\n<p><span style=\"font-size:14px;\">\u6700\u540e\u6709\u4eba\u8981\u95ee\u4e86: Python\u662f\u4e00\u79cd\u9ad8\u7ea7\u8bed\u8a00, \u4e3a\u5565\u8981\u505a\u8fd9\u4e48\u4f4e\u7ea7\u7684\u4e8b\u60c5\u5462? \u5176\u5b9e\u672f\u4e1a\u6709\u4e13\u653b, \u5bf9\u4e8e\u5d4c\u5165\u5f0f\u901a\u4fe1, \u7528python\u505a\u9ad8\u5c42\u7684\u8f85\u52a9\u6d4b\u8bd5\u5de5\u5177\u662f\u975e\u5e38\u65b9\u4fbf\u7684.<\/span><\/p>\n<p><span style=\"font-size:14px;\">(2015-12-14 \u8865\u5145)<strong>\u5c06\u5b57\u8282\u6d41\u704c\u6ce8\u5230\u7ed3\u6784\u4f53\u4e2d\u5b9e\u73b0\u89e3\u6790\u7684\u65b9\u6cd5:<\/strong><\/span><\/p>\n<p><span style=\"font-size:14px;\">r = Req()<\/span><br \/>\n<span style=\"font-size:14px;\">s = io_rcv() &nbsp; &nbsp; &nbsp; &nbsp;#receive byte stream from io<\/span><br \/>\n<span style=\"font-size:14px;\">memmove(addressof(r),s,sizeof(Req))<\/span><br \/>\n<span style=\"font-size:14px;\">&#8230;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\n\t<span style=\"font-size:14px;\"><a href=\"http:\/\/blog.csdn.net\/machael_sonic\/article\/details\/50266499\" target=\"_blank\">http:\/\/blog.csdn.net\/machael_sonic\/article\/details\/50266499<\/a><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><br \/>\n<\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\">1. \u7528C\/C++\u5b9e\u73b0\u7684\u7ed3\u6784\u5316\u6570\u636e\u5904\u7406<\/span>\n<\/p>\n<p>\n<span style=\"font-size:14px;\">\u5728\u6d89\u53ca\u5230\u6bd4\u8f83\u5e95\u5c42\u7684\u901a\u4fe1\u534f\u8bae\u5f00\u53d1\u8fc7\u7a0b\u4e2d, \u5f80\u5f80\u9700\u8981\u5f00\u53d1\u8bed\u8a00\u80fd\u591f\u6709\u6548\u7684\u8868\u8fbe\u548c\u5904\u7406\u6240\u5b9a\u4e49\u7684\u901a\u4fe1\u534f\u8bae\u7684\u6570\u636e\u7ed3\u6784. \u5728\u8fd9\u65b9\u9762\u662fC\/C++\u8bed\u8a00\u662f\u5177\u6709\u5929\u7136\u4f18\u52bf\u7684: \u901a\u8fc7struct, union, \u548cbit-fields, C\/C++\u80fd\u591f\u4ee5\u4e00\u79cd\u6700\u6709\u6548\u7387\u4e5f\u6700\u81ea&#8230;<\/span><\/p>\n","protected":false},"author":1,"featured_media":556,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[334],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[\u8f6c]ctypes+struct\u5b9e\u73b0\u7c7bc\u7684\u7ed3\u6784\u5316\u6570\u636e\u4e32\u884c\u5904\u7406 - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=215\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[\u8f6c]ctypes+struct\u5b9e\u73b0\u7c7bc\u7684\u7ed3\u6784\u5316\u6570\u636e\u4e32\u884c\u5904\u7406 - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"http:\/\/blog.csdn.net\/machael_sonic\/article\/details\/50266499      1. \u7528C\/C++\u5b9e\u73b0\u7684\u7ed3\u6784\u5316\u6570\u636e\u5904\u7406  \u5728\u6d89\u53ca\u5230\u6bd4\u8f83\u5e95\u5c42\u7684\u901a\u4fe1\u534f\u8bae\u5f00\u53d1\u8fc7\u7a0b\u4e2d, \u5f80\u5f80\u9700\u8981\u5f00\u53d1\u8bed\u8a00\u80fd\u591f\u6709\u6548\u7684\u8868\u8fbe\u548c\u5904\u7406\u6240\u5b9a\u4e49\u7684\u901a\u4fe1\u534f\u8bae\u7684\u6570\u636e\u7ed3\u6784. \u5728\u8fd9\u65b9\u9762\u662fC\/C++\u8bed\u8a00\u662f\u5177\u6709\u5929\u7136\u4f18\u52bf\u7684: \u901a\u8fc7struct, union, \u548cbit-fields, C\/C++\u80fd\u591f\u4ee5\u4e00\u79cd\u6700\u6709\u6548\u7387\u4e5f\u6700\u81ea...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=215\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-04-06T05:02:58+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/img.blog.csdn.net\/20151211103048615\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/weizn.net\/?p=215#primaryimage\",\"inLanguage\":\"zh-Hans\",\"url\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/shutterstock_684957946.jpg\",\"contentUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/shutterstock_684957946.jpg\",\"width\":1000,\"height\":667},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=215#webpage\",\"url\":\"http:\/\/weizn.net\/?p=215\",\"name\":\"[\\u8f6c]ctypes+struct\\u5b9e\\u73b0\\u7c7bc\\u7684\\u7ed3\\u6784\\u5316\\u6570\\u636e\\u4e32\\u884c\\u5904\\u7406 - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=215#primaryimage\"},\"datePublished\":\"2017-04-06T05:02:58+00:00\",\"dateModified\":\"2017-04-06T05:02:58+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=215#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=215\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=215#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[\\u8f6c]ctypes+struct\\u5b9e\\u73b0\\u7c7bc\\u7684\\u7ed3\\u6784\\u5316\\u6570\\u636e\\u4e32\\u884c\\u5904\\u7406\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=215#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=215#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"[\\u8f6c]ctypes+struct\\u5b9e\\u73b0\\u7c7bc\\u7684\\u7ed3\\u6784\\u5316\\u6570\\u636e\\u4e32\\u884c\\u5904\\u7406\",\"datePublished\":\"2017-04-06T05:02:58+00:00\",\"dateModified\":\"2017-04-06T05:02:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=215#webpage\"},\"wordCount\":1025,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"image\":{\"@id\":\"http:\/\/weizn.net\/?p=215#primaryimage\"},\"thumbnailUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/shutterstock_684957946.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=215#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[\u8f6c]ctypes+struct\u5b9e\u73b0\u7c7bc\u7684\u7ed3\u6784\u5316\u6570\u636e\u4e32\u884c\u5904\u7406 - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=215","og_locale":"zh_CN","og_type":"article","og_title":"[\u8f6c]ctypes+struct\u5b9e\u73b0\u7c7bc\u7684\u7ed3\u6784\u5316\u6570\u636e\u4e32\u884c\u5904\u7406 - Wayne&#039;s Blog","og_description":"http:\/\/blog.csdn.net\/machael_sonic\/article\/details\/50266499      1. \u7528C\/C++\u5b9e\u73b0\u7684\u7ed3\u6784\u5316\u6570\u636e\u5904\u7406  \u5728\u6d89\u53ca\u5230\u6bd4\u8f83\u5e95\u5c42\u7684\u901a\u4fe1\u534f\u8bae\u5f00\u53d1\u8fc7\u7a0b\u4e2d, \u5f80\u5f80\u9700\u8981\u5f00\u53d1\u8bed\u8a00\u80fd\u591f\u6709\u6548\u7684\u8868\u8fbe\u548c\u5904\u7406\u6240\u5b9a\u4e49\u7684\u901a\u4fe1\u534f\u8bae\u7684\u6570\u636e\u7ed3\u6784. \u5728\u8fd9\u65b9\u9762\u662fC\/C++\u8bed\u8a00\u662f\u5177\u6709\u5929\u7136\u4f18\u52bf\u7684: \u901a\u8fc7struct, union, \u548cbit-fields, C\/C++\u80fd\u591f\u4ee5\u4e00\u79cd\u6700\u6709\u6548\u7387\u4e5f\u6700\u81ea...","og_url":"http:\/\/weizn.net\/?p=215","og_site_name":"Wayne&#039;s Blog","article_published_time":"2017-04-06T05:02:58+00:00","og_image":[{"url":"http:\/\/img.blog.csdn.net\/20151211103048615"}],"twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"ImageObject","@id":"http:\/\/weizn.net\/?p=215#primaryimage","inLanguage":"zh-Hans","url":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/shutterstock_684957946.jpg","contentUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/shutterstock_684957946.jpg","width":1000,"height":667},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=215#webpage","url":"http:\/\/weizn.net\/?p=215","name":"[\u8f6c]ctypes+struct\u5b9e\u73b0\u7c7bc\u7684\u7ed3\u6784\u5316\u6570\u636e\u4e32\u884c\u5904\u7406 - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/weizn.net\/?p=215#primaryimage"},"datePublished":"2017-04-06T05:02:58+00:00","dateModified":"2017-04-06T05:02:58+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=215#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=215"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=215#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"[\u8f6c]ctypes+struct\u5b9e\u73b0\u7c7bc\u7684\u7ed3\u6784\u5316\u6570\u636e\u4e32\u884c\u5904\u7406"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=215#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=215#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"[\u8f6c]ctypes+struct\u5b9e\u73b0\u7c7bc\u7684\u7ed3\u6784\u5316\u6570\u636e\u4e32\u884c\u5904\u7406","datePublished":"2017-04-06T05:02:58+00:00","dateModified":"2017-04-06T05:02:58+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=215#webpage"},"wordCount":1025,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"image":{"@id":"http:\/\/weizn.net\/?p=215#primaryimage"},"thumbnailUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/shutterstock_684957946.jpg","articleSection":["Python"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=215#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/media\/556"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}