{"id":2052,"date":"2023-04-01T21:17:46","date_gmt":"2023-04-01T13:17:46","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2052"},"modified":"2023-04-07T06:05:59","modified_gmt":"2023-04-06T22:05:59","slug":"c-language-implementation-of-mutual-conversion-between-floating-point-data-and-byte-arrays","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/c-language-implementation-of-mutual-conversion-between-floating-point-data-and-byte-arrays\/","title":{"rendered":"C\u8bed\u8a00\u5b9e\u73b0\u6d6e\u70b9\u578b\u6570\u636e\u4e0e\u5b57\u8282\u6570\u7ec4\u7684\u76f8\u4e92\u8f6c\u5316"},"content":{"rendered":"<p>\u5728\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u6216\u7f51\u7edc\u901a\u4fe1\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u5f62\u5f0f\u4f20\u8f93\uff0c\u6b64\u65f6\u9700\u8981\u8fdb\u884c\u6570\u636e\u683c\u5f0f\u8f6c\u6362\u3002<\/p>\n<p>MCU\u548cPC\u7684\u6d6e\u70b9\u6570\u90fd\u662f\u57fa\u4e8eIEEE754\u683c\u5f0f\u7684\uff0c\u67094\u5b57\u8282(float)\u30018\u5b57\u8282\uff08double\uff09\u3002\u4ee5\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u578b\u6570\u636e\u4e3a\u4f8b\uff0c\u5206\u6790\u5f3a\u5236\u6307\u9488\u7c7b\u578b\u8f6c\u6362\u7684\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nvoid double2bytes(double data, unsigned char* bytes);\ndouble bytes2double(unsigned char bytes[]);\n\nint main()\n{\n    double d1 = 3.14159;\n    unsigned char b1[8];\n    double2bytes(d1, b1);\n    int i;\n    for(i=0; i&lt;8; i++)\n    {\n        printf(&quot;%02X &quot;, b1[i]);\n    }\n\n    printf(&quot;\\n&quot;);\n\n    unsigned char b2[8] = {0x6E, 0x86, 0x1B, 0xF0, 0xF9, 0x21, 0x09, 0x40};\n    double d2 = bytes2double(b2);\n    printf(&quot;%f&quot;, d2);\n\n    return 0;\n}\n\n\/**\n * \u53cc\u7cbe\u5ea6\u6d6e\u70b9\u578b\u6570\u636edouble\u8f6c\u5b57\u8282\u6570\u7ec4\uff08\u5c0f\u7aef\u6a21\u5f0f\uff09\n *\/\nvoid double2bytes(double data, unsigned char bytes[])\n{\n    int i;\n    char* p = (char*)&amp;data;  \/\/\u5c06double\u7c7b\u578b\u7684\u6307\u9488\u5f3a\u5236\u8f6c\u6362\u4e3aunsigned char\u578b\n    for(i=0; i&lt;8; i++)\n    {\n        bytes[i] = *p++;  \/\/\u5c06\u76f8\u5e94\u5730\u5740\u4e2d\u7684\u6570\u636e\u4fdd\u5b58\u5230unsigned char\u6570\u7ec4\u4e2d\n    }\n}\n\n\/**\n * \u5b57\u8282\u6570\u7ec4\u8f6c\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u578b\u6570\u636edouble\uff08\u5c0f\u7aef\u6a21\u5f0f\uff09\n *\/\ndouble bytes2double(unsigned char bytes[])\n{\n    double data = *((double *)bytes);  \/\/\u76f4\u63a5\u5c06unsigned char\u578b\u7684\u6307\u9488\u5f3a\u5236\u8f6c\u6362\u4e3adouble\u7c7b\u578b\n    return data;\n}\n\n\u8f93\u51fa\uff1a\n6E 86 1B F0 F9 21 09 40 \n3.141590<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u6216\u7f51\u7edc\u901a\u4fe1\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u5f62\u5f0f\u4f20\u8f93\uff0c\u6b64\u65f6\u9700\u8981\u8fdb\u884c\u6570\u636e\u683c\u5f0f\u8f6c\u6362\u3002 MCU\u548cPC\u7684\u6d6e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[517],"tags":[],"class_list":["post-2052","post","type-post","status-publish","format-standard","hentry","category-c-cpp"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2052"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2052\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}