{"id":843,"date":"2021-12-27T03:45:00","date_gmt":"2021-12-27T03:45:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=843"},"modified":"2021-12-27T03:45:00","modified_gmt":"2021-12-27T03:45:00","slug":"843","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/12\/27\/843\/","title":{"rendered":"Python\u8bfb\u5199C\u8bed\u8a00\u6570\u636e\u7ed3\u6784 &#8211; ctypes \u548c struct \u5e93"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/1250036530.webp\" alt=\"python.webp\" title=\"python.webp\"><\/p>\n<h2>Python \u7684\u5916\u90e8\u51fd\u6570\u5e93 <a href=\"https:\/\/lyvba.com\/python_doc\/library\/ctypes.html?highlight=ctypes\">ctypes<\/a><\/h2>\n<ul>\n<li>ctypes \u662f Python \u7684\u5916\u90e8\u51fd\u6570\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e0e C \u517c\u5bb9\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u5141\u8bb8\u8c03\u7528 DLL \u6216\u5171\u4eab\u5e93\u4e2d\u7684\u51fd\u6570\u3002\u53ef\u4f7f\u7528\u8be5\u6a21\u5757\u4ee5\u7eaf Python \u5f62\u5f0f\u5bf9\u8fd9\u4e9b\u5e93\u8fdb\u884c\u5c01\u88c5\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/3283790503.png\" alt=\"ctypes.png\" title=\"ctypes.png\"><\/p>\n<h3>\u672c\u6587\u53ea\u7528\u5230 <code>ctypes<\/code> \u6a21\u5757\u4e2d\u7684\u7ed3\u6784\u4f53 <code>Structure<\/code> \u4e0e C \u517c\u5bb9\u6570\u636e\u7c7b\u578b<\/h3>\n<h3>Python \u7684\u5916\u90e8\u51fd\u6570\u5e93 <code>ctypes<\/code> \u4e4b <a href=\"https:\/\/lyvba.com\/python_doc\/library\/ctypes.html?highlight=ctypes#Structure\">\u7ed3\u6784\u4f53\u548c\u8054\u5408<\/a><\/h3>\n<p>\u7ed3\u6784\u4f53\u548c\u8054\u5408\u5fc5\u987b\u7ee7\u627f\u81ea ctypes \u6a21\u5757\u4e2d\u7684 <code>Structure<\/code> \u548c Union \u3002\u5b50\u7c7b\u5fc5\u987b\u5b9a\u4e49 <em>fields<\/em> \u5c5e\u6027\u3002 <em>fields<\/em> \u662f\u4e00\u4e2a\u4e8c\u5143\u7ec4\u5217\u8868\uff0c\u4e8c\u5143\u7ec4\u4e2d\u5305\u542b field name \u548c field type \u3002<\/p>\n<p>type \u5b57\u6bb5\u5fc5\u987b\u662f\u4e00\u4e2a ctypes \u7c7b\u578b\uff0c\u6bd4\u5982 c_int\uff0c\u6216\u8005\u5176\u4ed6 ctypes \u7c7b\u578b: \u7ed3\u6784\u4f53\u3001\u8054\u5408\u3001\u6570\u7ec4\u3001\u6307\u9488\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684 POINT \u7ed3\u6784\u4f53\uff0c\u5b83\u5305\u542b\u540d\u79f0\u4e3a x \u548c y \u7684\u4e24\u4e2a\u53d8\u91cf\uff0c\u8fd8\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u3002<\/p>\n<pre><code>from ctypes import *\nclass POINT(Structure):\n    _fields_ = [(\"x\", c_int),\n                (\"y\", c_int)]\n\npoint = POINT(10, 20)\nprint(point.x, point.y)\n\npoint = POINT(y=5)\nprint(point.x, point.y)\n\nPOINT(1, 2, 3)<\/code><\/pre>\n<h2>python\u8bfb\u5199C\u8bed\u8a00\u6570\u636e\u7ed3\u6784.py  <a href=\"https:\/\/github.com\/hongwenjun\/srgb\/blob\/master\/python\/python%E8%AF%BB%E5%86%99C%E8%AF%AD%E8%A8%80%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.py\">\u5b9e\u9a8c\u6e90\u7801<\/a><\/h2>\n<h3>C \u8bed\u8a00\u5b9a\u4e49\u7684\u7ed3\u6784\u4f53 <code>struct gps<\/code> \u5728Python\u4e2d\u5bf9\u5e94\u5199\u6210 <code>class Location(Structure)<\/code><\/h3>\n<pre><code>struct gps {\n  double x; double y; };\n\nclass Location(Structure):\n    _fields_ = [('lon', c_double), ('lat', c_double)]<\/code><\/pre>\n<h3>pybin.py <code>Python<\/code> \u4f7f\u7528 C\u6570\u636e\u7ed3\u6784\uff0c\u628a\u4e00\u4e2agps\u5750\u6807\u5199\u5230\u6587\u4ef6\u4e2d\u3002<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/3595420320.png\" alt=\"pybin.py\" title=\"pybin.py\"><\/p>\n<h3>fread.c  \u4f7f\u7528C\u8bed\u8a00\u7a0b\u5e8f\u8bfb\u53d6\u6570\u636e\u6587\u4ef6\uff0c\u9a8c\u8bc1\u5199\u6587\u4ef6\u662f\u5426\u6b63\u786e<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/2157736220.png\" alt=\"fread.c\" title=\"fread.c\"><\/p>\n<h3>pybin_read.py <code>Python<\/code> \u8bfb\u53d6C\u6570\u636e\u7ed3\u6784\u6587\u4ef6\u540e\uff0c\u9700\u8981\u4f7f\u7528 <code>struct<\/code> \u5e93\u8fdb\u884c\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/1237786508.png\" alt=\"pybin_read.py\" title=\"pybin_read.py\"><\/p>\n<h2>\u5c06\u5b57\u8282\u4e32\u89e3\u8bfb\u4e3a\u6253\u5305\u7684\u4e8c\u8fdb\u5236\u6570\u636e <a href=\"https:\/\/lyvba.com\/python_doc\/library\/struct.html\">struct<\/a><\/h2>\n<ul>\n<li>\u6b64\u6a21\u5757\u53ef\u4ee5\u6267\u884c Python \u503c\u548c\u4ee5 Python bytes \u5bf9\u8c61\u8868\u793a\u7684 C \u7ed3\u6784\u4e4b\u95f4\u7684\u8f6c\u6362\u3002 <br \/>\u8fd9\u53ef\u4ee5\u88ab\u7528\u6765\u5904\u7406\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u6216\u662f\u4ece\u7f51\u7edc\u8fde\u63a5\u7b49\u5176\u4ed6\u6765\u6e90\u83b7\u53d6\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u3002<br \/>\u5b83\u4f7f\u7528 \u683c\u5f0f\u5b57\u7b26\u4e32 \u4f5c\u4e3a C \u7ed3\u6784\u5e03\u5c40\u7684\u7cbe\u7b80\u63cf\u8ff0\u4ee5\u53ca\u4e0e Python \u503c\u7684\u53cc\u5411\u8f6c\u6362\u3002<\/li>\n<\/ul>\n<pre><code>aa = struct.pack(\"4B\", 1, 2, 3, 4)\nprint(aa)\nbb = struct.unpack(\"f\", aa)\nprint(bb)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/4004446017.png\" alt=\"fmt.png\" title=\"fmt.png\"><\/p>\n<h2>Python\u8bfb\u5199C\u8bed\u8a00\u6570\u636e\u7ed3\u6784 \u603b\u7ed3\u7cbe\u7b80<\/h2>\n<pre><code>import struct\nfrom ctypes import *\nclass Location(Structure):\n    _fields_ = [('lon', c_double), ('lat', c_double)]\n\nf = open(\"gps.dat\", \"rb\")\nt = f.read(16) ; print(t)\nt = struct.unpack(\"2d\", t) ; print(t)\ngps =Location(lon=t[0], lat=t[1])\nprint(gps.lon, gps.lat)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7684\u5916\u90e8\u51fd\u6570\u5e93 ctypes ctypes \u662f Python \u7684\u5916\u90e8\u51fd\u6570\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e0e  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-843","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=843"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/843\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}