{"id":131,"date":"2019-09-17T20:52:16","date_gmt":"2019-09-17T12:52:16","guid":{"rendered":"http:\/\/www.sanburs.xyz\/?p=131"},"modified":"2019-09-17T20:52:16","modified_gmt":"2019-09-17T12:52:16","slug":"twousageofunion","status":"publish","type":"post","link":"https:\/\/www.sanburs.xyz\/index.php\/2019\/09\/17\/twousageofunion\/","title":{"rendered":"union\u7684\u4e8c\u79cd\u7528\u6cd5"},"content":{"rendered":"<p>\u8054\u5408\u4f53(union)\u5171\u7528\u4e00\u4e2a\u5185\u5b58\u9996\u5730\u5740\uff0c\u5e76\u4e14\u5404\u79cd\u53d8\u91cf\u540d\u90fd\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\uff0c\u64cd\u4f5c\u4e5f\u662f\u5171\u540c\u751f\u6548\u3002<\/p>\n<h2>\u7b2c\u4e00\u79cd<\/h2>\n<p>\u4e0b\u8fb9\u793a\u8303\u4e86\u4e00\u79cd\u7528\u9014\uff0c\u4ee3\u8868\u56db\u4e2a\u542b\u4e49\u7684\u56db\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u53ef\u4ee5\u7528\u4e00\u4e2aint\u6765\u64cd\u4f5c\uff0c\u76f4\u63a5int\u8d4b\u503c\uff0c\u65e0\u8bba\u5185\u5b58\u8bbf\u95ee\uff08\u6307\u9488\u5927\u5c0f\u7684\u6574\u6570\u500d\uff0c\u8bbf\u95ee\u624d\u6709\u6548\u7387\uff09\uff0c\u8fd8\u662f\u65f6\u95f4\u590d\u6742\u5ea6\uff08\u4e00\u6b21\u548c\u56db\u6b21\u7684\u533a\u522b\uff0c\u800c\u4e14\u8fd9\u56db\u6b21\u6709\u4e09\u6b21\u90fd\u662f\u4e0d\u6574\u9f50\u7684\u5730\u5740\uff09\uff0c\u90fd\u4f1a\u4f4e\u4e00\u4e9b\u3002<\/p>\n<pre><code class=\"language-c \">#include&lt;stdio.h&gt;  \nunion var{  \n        char c[4];  \n        int i;  \n};  \n\nint main(){  \n        union var data;  \n        data.c[0] = 0x04;\/\/\u56e0\u4e3a\u662fchar\u7c7b\u578b\uff0c\u6570\u5b57\u4e0d\u8981\u592a\u5927\uff0c\u7b97\u7b97ascii\u7684\u8303\u56f4~  \n        data.c[1] = 0x03;\/\/\u5199\u621016\u8fdb\u5236\u4e3a\u4e86\u65b9\u4fbf\u76f4\u63a5\u6253\u5370\u5185\u5b58\u4e2d\u7684\u503c\u5bf9\u6bd4  \n        data.c[2] = 0x02;  \n        data.c[3] = 0x11;  \n\/\/\u6570\u7ec4\u4e2d\u4e0b\u6807\u4f4e\u7684\uff0c\u5730\u5740\u4e5f\u4f4e\uff0c\u6309\u5730\u5740\u4ece\u4f4e\u5230\u9ad8\uff0c\u5185\u5b58\u5185\u5bb9\u4f9d\u6b21\u4e3a\uff1a04,03,02,11\u3002\u603b\u5171\u56db\u5b57\u8282\uff01  \n\/\/\u800c\u628a\u56db\u4e2a\u5b57\u8282\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\uff08\u4e0d\u5206\u7c7b\u578b\uff0c\u76f4\u63a5\u6253\u5370\u5341\u516d\u8fdb\u5236\uff09\uff0c\u5e94\u8be5\u4ece\u5185\u5b58\u9ad8\u5730\u5740\u5230\u4f4e\u5730\u5740\u770b\uff0c0x11020304\uff0c\u4f4e\u4f4d04\u653e\u5728\u4f4e\u5730\u5740\u4e0a\u3002  \n        printf(\"%x\\n\",data.i);  \n}  \n<\/code><\/pre>\n<p>\u5047\u8bbe\u7ed3\u679c\uff1a<br \/>\n11020304<br \/>\n\u5219\u8bc1\u660e\u8fd9\u4e2a32\u4f4dlinux\u662f\u5c0f\u7aef\uff08little-endian\uff09<\/p>\n<h2>\u7b2c\u4e8c\u79cd<\/h2>\n<p>\u4e0b\u9762\u7684\u7a0b\u5e8f\u662f\u7c73\u79d1\u7684\u6d41\u91cf\u8ba1\u4e2d\u6570\u636e\u5904\u7406\u7684\u4f8b\u7a0b\uff0cC\u8bed\u8a00\u7248\u672c\u7684\u3002<\/p>\n<pre><code class=\"language-c \">void V77_Instant(void)\n{\nsigned long ddd,eee;\nfloat temp;\nWriteMenu(0,0,0,8,0);\nWriteMenu(4,0,0,9,0);\nunion ieee754_to_float\n{\nunsigned char IEE[4];\nfloat FLOA;\n};\nunion ieee754_to_float Change;\nChange.IEE[3] = RS485_Recievebuf[3]; \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\u77ac\u65f6\u6d41\u91cf\nChange.IEE[2] = RS485_Recievebuf[4];\nChange.IEE[1] = RS485_Recievebuf[5];\nChange.IEE[0] = RS485_Recievebuf[6];\ntemp = Change.FLOA;\nddd = (signed long)(temp*100000);\ndisp_511_Number(2,0,ddd,10,5);\/\/\u663e\u793a\u77ac\u65f6\u6d41\u91cf\nswitch(RS485_Recievebuf[36]) \/\/\u663e\u793a\u77ac\u65f6\u6d41\u91cf\u5355\u4f4d\n{\ncase 0x00:WriteMenu(2,96,0,13,0);break;\ncase 0x01:WriteMenu(2,96,0,14,0);break;\ncase 0x02:WriteMenu(2,96,0,15,0);break;\ncase 0x03:WriteMenu(2,96,0,16,0);break;\ncase 0x04:WriteMenu(2,96,0,17,0);break;\ncase 0x05:WriteMenu(2,96,0,18,0);break;\ncase 0x06:WriteMenu(2,96,0,19,0);break;\ncase 0x07:WriteMenu(2,96,0,20,0);break;\ncase 0x08:WriteMenu(2,96,0,21,0);break;\ndefault:break;\n}\nChange.IEE[3] = RS485_Recievebuf[7]; \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\u77ac\u65f6\u6d41\u901f\nChange.IEE[2] = RS485_Recievebuf[8];\nChange.IEE[1] = RS485_Recievebuf[9];\nChange.IEE[0] = RS485_Recievebuf[10];\ntemp = Change.FLOA;\neee = (signed long)(temp*100000);\ndisp_511_Number(6,0,eee,10,5);\/\/\u663e\u793a\u77ac\u65f6\u6d41\u901f\nWriteMenu(6,96,0,22,0); \/\/\u663e\u793a\u77ac\u65f6\u6d41\u901f\u5355\u4f4d\n}\n\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u6709\u6bb5union\u7684\u4f7f\u7528\u3002\u5efa\u7acb\u4e00\u4e2aunion\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<br \/>\n\u901a\u8fc7485\u4e32\u53e3\u8bfb\u51fa\u6765\u7684buff\u8f6c\u6362\u4e3afloat<\/p>\n<pre><code class=\"language-C \">union ieee754_to_float\n{\nunsigned char IEE[4];\nfloat FLOA;\n};\nunion ieee754_to_float Change;\nChange.IEE[3] = RS485_Recievebuf[3]; \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\u77ac\u65f6\u6d41\u91cf\nChange.IEE[2] = RS485_Recievebuf[4];\nChange.IEE[1] = RS485_Recievebuf[5];\nChange.IEE[0] = RS485_Recievebuf[6];\ntemp = Change.FLOA;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8054\u5408\u4f53(union)\u5171\u7528\u4e00\u4e2a\u5185\u5b58\u9996\u5730\u5740\uff0c\u5e76\u4e14\u5404\u79cd\u53d8\u91cf\u540d\u90fd\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\uff0c\u64cd\u4f5c\u4e5f\u662f\u5171\u540c\u751f\u6548\u3002 \u7b2c\u4e00\u79cd \u4e0b\u8fb9\u793a\u8303\u4e86\u4e00\u79cd\u7528\u9014\uff0c\u4ee3\u8868\u56db\u4e2a\u542b\u4e49 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/131"}],"collection":[{"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":2,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/131\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sanburs.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}