{"id":132,"date":"2021-09-22T14:40:54","date_gmt":"2021-09-22T06:40:54","guid":{"rendered":"https:\/\/blog.xiaotao2333.top:344\/?p=132"},"modified":"2021-10-13T16:51:10","modified_gmt":"2021-10-13T08:51:10","slug":"%e7%90%86%e8%a7%a3%e5%a4%a7%e7%ab%af%e5%ba%8f%e5%92%8c%e5%b0%8f%e7%ab%af%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/blog.xiaotao2333.top:344\/?p=132","title":{"rendered":"\u7406\u89e3\u5927\u7aef\u5e8f\u548c\u5c0f\u7aef\u5e8f"},"content":{"rendered":"<h1>\u7406\u89e3\u5927\u7aef\u5e8f\u548c\u5c0f\u7aef\u5e8f<\/h1>\n<h2>\u5f00\u59cb\u4e4b\u524d<\/h2>\n<p>\u6587\u7ae0\u6b63\u5f0f\u5f00\u59cb\u4e4b\u524d\uff0c\u5148\u6765\u505a\u4e00\u4e2a\u5c0f\u6d4b\u8bd5<\/p>\n<h3>\u524d\u63d0<\/h3>\n<p>\u5df2\u77e5\u8fd0\u884c\u5e73\u53f0\uff08x86_64\uff0cLinux\uff0cgcc 9.3.0\uff09\u4e2d\uff0c\u4e00\u4e2aint\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u662f4Byte\u3002<br \/>\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2aint\u7c7b\u578b\u7684\u6570\u636e\u4f1a\u5360\u7528\u5185\u5b58\u4e2d\u4e00\u5757\u8fde\u7eed4Byte\u5927\u5c0f\u7684\u533a\u57df\u3002<br \/>\n\u6709\u8fd9\u4e48\u4e00\u4e2aint\u5341\u8fdb\u5236\u6574\u6570\uff1a<code>305419896<\/code>\uff0c\u7528\u5341\u516d\u8fdb\u5236\u8868\u793a\u7684\u8bdd\u5c31\u662f<code>0x12345678<\/code><\/p>\n<p>\u4f7f\u75284Byte(32bit) \u7684\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u8bdd\uff0c\u5c31\u662f<\/p>\n<table>\n<thead>\n<tr>\n<th>[0]<\/th>\n<th>[1]<\/th>\n<th>[2]<\/th>\n<th>[3]<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>00010010  (0x12)<\/td>\n<td>00110100  (0x34)<\/td>\n<td>01010110  (0x56)<\/td>\n<td>01111000  (0x78)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>C\u8bed\u8a00\u7684\u5185\u5b58\u9b54\u6cd5<\/h3>\n<p>\u65e2\u7136\u4e00\u4e2aint\u662f\u5360\u4e864\u4e2aByte\uff0c\u90a3\u4e48\u6709\u6ca1\u6709\u529e\u6cd5\u628a\u6bcf\u4e2aByte\u7684\u5185\u5bb9\u5355\u72ec\u53d6\u51fa\u6765\u800c\u4e0d\u662f\u628a4\u4e2aByte\u4f5c\u4e3a\u4e00\u4e2aint\u6574\u4f53\u5462\uff1f<\/p>\n<p>\u5f53\u7136\u53ef\u4ee5<\/p>\n<p>\u8fd9\u65f6\u5019\u5c31\u8981\u7528\u5230C\u8bed\u8a00\u795e\u5947\u7684\u6307\u9488\u548c\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u7684<\/p>\n<pre><code class=\"language-c\">\/\/ \u5e73\u5e73\u65e0\u5947\u7684\u5341\u516d\u8fdb\u5236\u5199\u6cd5\u7684int\u6574\u6570\nint a = 0x12345678;\n\n\/\/ \u83b7\u53d6\u53d8\u91cfa\u7684\u6307\u9488\uff0c\u5e76\u628a\u6307\u9488\u7c7b\u578b\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u6210unsigned char*\u578b\nunsigned char * p = (unsigned char *)&amp;a;\n\nfor(int i = 0; i &lt; sizeof(int); i++) {\n    printf(&quot;%x &quot;, p[i]); \/\/ \u8f93\u51fa\u53d8\u91cfa\u5185\u5b58\u5730\u5740\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\uff0c\u6bcfByte\u7684\u5341\u516d\u8fdb\u5236\u5185\u5bb9\n}<\/code><\/pre>\n<p><code>unsigned char * p = (unsigned char *)&amp;a;<\/code>\u8fd9\u4e48\u505a\u7684\u8bb2\u7a76\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u76ee\u7684\u662f\u56e0\u4e3aint\u6307\u9488\u7684\u6bcf\u6b21\u505a\u52a0\u51cf\u6cd5\u8fd0\u7b97\u65f6\uff0c\u5730\u5740\u504f\u79fb\u7684\u5355\u4f4d\u90fd\u662f4\uff0c\u800cchar\u578b\u6307\u9488\u5730\u5740\u6bcf\u6b21\u504f\u79fb\u7684\u91cf\u662f1\uff0c\u8fd9\u6837\u5c31\u80fd\u5b9e\u73b0\u5bf9\u4e00\u4e2a\u957f\u5ea6\u4e3a4Byte\u7684int\u5185\u5b58\u533a\u57df\u6309\u6bcf1Byte\u8fdb\u884c\u64cd\u4f5c\u4e86<\/p>\n<p>\u8f93\u51fa\u5185\u5bb9\u662f\u5565\uff1f<br \/>\n\u4e5f\u8bb8\u4f60\u4ee5\u4e3a\u662f<code>0x12 0x34 0x56 0x78<\/code>\u5427\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\u662f\uff1a<code>0x78 0x56 0x34 0x12<\/code><\/p>\n<p><img src=\"https:\/\/blog.xiaotao233.top\/wp-content\/uploads\/2021\/09\/\u5c0f\u7aef\u5e8f\u6f14\u793a.jpg\" alt=\"\" \/><\/p>\n<h2>\u5b57\u8282\u5e8f<\/h2>\n<h3>\u5927\u5c0f\u7aef\u5e8f<\/h3>\n<p>\u9707\u60ca\uff0c\u5c45\u7136\u662f\u548c\u6211\u4eec\u4eba\u7c7b\u76f4\u89c2\u7684\u505a\u6cd5\u662f\u5012\u7740\u6765\u7684\u3002<\/p>\n<p>\u50cf\u4e0a\u9762\u8fd9\u79cd\u5bf9\u4eba\u7c7b\u6765\u8bf4\uff0c\u672c\u5e94\u8be5\u5728\u5185\u5b58\u6700\u9ad8\u4f4d\u7684\u6570\u636e\uff0c\u88ab\u653e\u5230\u5185\u5b58\u6700\u4f4e\u4f4d\u7684\u5b57\u8282\u987a\u5e8f\uff0c\u5c31\u88ab\u79f0\u4e3a<strong>\u5c0f\u7aef\u5e8f<\/strong>\uff08Little-Endian\uff09\u3002<\/p>\n<p>\u76f8\u53cd\uff0c\u548c\u6211\u4eec\u4eba\u7c7b\u76f4\u89c2\u76f4\u89c9\u76f8\u7b26\u5408\u7684\uff0c\u5c31\u662f<strong>\u5927\u7aef\u5e8f<\/strong>\u4e86\uff08Big-Endian\uff09<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u8981\u6709\u5b57\u8282\u5e8f<\/h3>\n<p>\u8ba1\u7b97\u673a\u5185\u90e8\u57fa\u672c\u7684\u5b58\u50a8\u5355\u4f4d\u662fByte\uff08\u5b57\u8282\uff09\uff0c1Byte = 8bit\uff0c\u80fd\u5b58\u50a8\u7684\u5927\u5c0f\u76f8\u5f53\u6709\u9650\uff0c\u6570\u636e\u4e4b\u5927\uff0c1Byte\u88c5\u4e0d\u4e0b\u7684\u65f6\u5019\uff0c\u5c31\u8981\u7528\u597d\u51e0\u4e2aByte\u4e00\u8d77\u53bb\u5b58\uff0c\u6bd4\u59821\u4e2aint\uff0c\u5c31\u7528\u5230\u4e864\u4e2aByte\u53bb\u5b58\u50a8\u3002<br \/>\n\u65e2\u7136\u4f60\u7528\u4e86\u8fde\u7eed\u591a\u4e2aByte\u5b58\u50a8\uff0c\u603b\u5f97\u6709\u4e2a\u5148\u540e\u987a\u5e8f\u5bf9\u5427\uff0c\u6240\u4ee5\u8fd9\u5c31\u4ea7\u751f\u4e86\u201c\u5b57\u8282\u5e8f\u201d\u7684\u6982\u5ff5\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u6211\u4eec\u7684\u8ba1\u7b97\u673a\u91c7\u7528\u4e86\u5c0f\u7aef\u5e8f<\/h3>\n<p>\u5bf9\u4e8e\u6211\u4eec\u4eba\u7c7b\u6765\u8bf4\uff0c\u8fdb\u884c\u52a0\u51cf\u4e58\u9664\u56db\u5219\u8fd0\u7b97\u7684\u65f6\u5019\uff0c\u90fd\u662f\u4ece\u4e2a\u4f4d\u6570\u5f00\u59cb\u8ba1\u7b97<\/p>\n<p>\u6bd4\u598212345 + 67\uff0c\u5148\u7b97\u4e2a\u4f4d5+7=12\uff0c\u518d\u7b97\u5341\u4f4d4+6+1\uff08\u8fdb\u4f4d\uff09=11\uff0c\u6700\u540e\u7b97\u767e\u4f4d3+1(\u8fdb\u4f4d)=4\uff0c\u6700\u7ec8\u7ed3\u679c12412<\/p>\n<p>\u6b38\uff1f\u53d1\u73b0\u4e86\u6ca1\uff1f\u867d\u7136\u6211\u4eec\u6570\u5b57\u4e66\u5199\u662f\u4ece\u5de6\u5f80\u53f3\uff0c\u4f46\u6211\u4eec\u8fd0\u7b97\u7684\u65f6\u5019\uff0c\u662f\u4ece\u53f3\u5f80\u5de6\uff0c\u4e5f\u5c31\u662f\u5148\u4ece\u4f4e\u4f4d\u5f00\u59cb\u8ba1\u7b97\u3002\u800c\u6b63\u597d\u6211\u4eec\u7684\u8ba1\u7b97\u673a\u7535\u8def\u5728\u5b58\u53d6\u6570\u636e\u7684\u65f6\u5019\uff0c\u4e5f\u662f\u4ece\u5730\u5740\u7684\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\uff0c\u4e8e\u662f\u4e4e\u79d1\u5b66\u5bb6\u4eec\u5e72\u8106\u76f4\u63a5\u5c31\u8ba9\u8fd9\u79cd\u5927\u4e8e1Byte\u7684\u6570\u636e\u91c7\u7528\u5c0f\u7aef\u5e8f\u5728\u8ba1\u7b97\u673a\u4e2d\u8fdb\u884c\u5b58\u50a8\u548c\u8fd0\u7b97\u3002<\/p>\n<h3>\u6240\u6709\u8ba1\u7b97\u673a\/\u8ba1\u7b97\u673a\u7684\u4e00\u5207\u90fd\u662f\u5c0f\u7aef\u5e8f\u4e48\uff1f<\/h3>\n<p>\u5f53\u7136\u4e0d\u662f\uff0cPowerPC\u3001IBM\u3001Sun\u5c31\u662f\u91c7\u7528\u5927\u7aef\u5e8f\u5b58\u50a8\u7684\uff0c\u800cx86\u3001DEC\u5219\u91c7\u7528\u4e86\u5c0f\u7aef\u5e8f\u3002<\/p>\n<p>\u5927\u7aef\u5e8f\u7684\u597d\u5904\uff1a\u5185\u5b58\u4f4e\u4f4d\u7b2c\u4e00\u4e2a\u5c31\u80fd\u8bfb\u5230\u7b26\u53f7\u4f4d\uff0c\u5224\u65ad\u6b63\u8d1f\u5f88\u5feb\u554a\uff01\uff08\u9a6c\u8001\u5e08\u914d\u97f3\uff09<br \/>\n\u5c0f\u7aef\u5e8f\u7684\u597d\u5904\uff1a\u524d\u9762\u8bf4\u7684\u8fd0\u7b97\u9ad8\u6548\u65b9\u4fbf\uff0c\u7c7b\u578b\u5f3a\u8f6c\u4e0d\u9700\u8981\u91cd\u65b0\u8c03\u6574\uff08\u6bd4\u5982int\u8f6cshort\uff0c\u4f4e\u4f4d\u90fd\u4e0d\u9700\u8981\u52a8\u7684\uff0c\u9ad8\u4f4d\u4e0d\u7406\u5c31\u5b8c\u4e8b\u4e86\uff09<\/p>\n<p>\u800c\u5bf9\u4e8e\u7f51\u7edc\u534f\u8bae\u4e2d\u4f7f\u7528\u7684\u5b57\u8282\u5e8f\uff0c\u5219\u7edf\u4e00\u89c4\u8303\u4f7f\u7528\u4e86\u5927\u7aef\u5e8f\uff0c\u6240\u4ee5\u6784\u9020\u548c\u89e3\u6790\u7f51\u7edc\u534f\u8bae\u62a5\u6587\u7684\u65f6\u5019\uff0c\u5c31\u5f97\u6839\u636e\u4e3b\u673a\u81ea\u5df1\u7684\u5b57\u8282\u5e8f\u6765\u8fdb\u884c\u5fc5\u8981\u7684\u8f6c\u6362\u4e86\u3002<\/p>\n<h3>\u672c\u673a\u5b57\u8282\u5e8f\u7684\u5224\u65ad<\/h3>\n<p>\u4f7f\u7528C\u8bed\u8a00\u4ee3\u7801\u7b80\u5355\u5b9e\u73b0<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#define BIGENDIAN 1\n#define LITTLEENDIAN 0\nint get_byte_seq() {\n    int static flag = -1; \/\/ \u7ed3\u679c\u7f13\u5b58\uff0c\u591a\u6b21\u8c03\u7528\u5c31\u4e0d\u9700\u8981\u505a\u91cd\u590d\u8fd0\u7b97\u4e86\n    if (flag == -1) {\n        int a = 0x12345678;\n        unsigned char * b = (unsigned char *)&amp;a;\n        if (b[0] == 0x78) { \/\/ \u4f4e\u4f4d\u7684\u5185\u5b58\u5730\u5740\u662f\u6570\u636e\u7684\u4f4e\u4f4d\u503c\uff0c\u5c31\u662f\u5c0f\u7aef\u5e8f\n            flag = LITTLEENDIAN;\n        } else {\n            flag = BIGENDIAN;\n        }\n    }\n    return flag;\n}\n\nint main(int argc, char const *argv[])\n{\n    printf(&quot;\u5b57\u8282\u5e8f\u7c7b\u578b\uff1a%s\\n&quot;, get_byte_seq() == BIGENDIAN ? &quot;\u5927\u7aef\u5e8f&quot; : &quot;\u5c0f\u7aef\u5e8f&quot;);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7406\u89e3\u5927\u7aef\u5e8f\u548c\u5c0f\u7aef\u5e8f \u5f00\u59cb\u4e4b\u524d \u6587\u7ae0\u6b63\u5f0f\u5f00\u59cb\u4e4b\u524d\uff0c\u5148\u6765\u505a\u4e00\u4e2a\u5c0f\u6d4b\u8bd5 \u524d\u63d0 \u5df2\u77e5\u8fd0\u884c\u5e73\u53f0\uff08x86_64\uff0cLinux [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":136,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[37,36,35,34],"_links":{"self":[{"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=\/wp\/v2\/posts\/132"}],"collection":[{"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=132"}],"version-history":[{"count":9,"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions\/152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=\/wp\/v2\/media\/136"}],"wp:attachment":[{"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xiaotao2333.top:344\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}