测试机器大小端

intel32的机器,虽然是64位的系统,

但,


short是2个字节啊。


大端:低地址取出来的是高位字节。这是网络字节序。

#include
#include 
void byteorder()
{
     union{
        short value;
        char union_bytes[sizeof(short)];
     }test;
     printf("sizeof(short) is [%d]\n",sizeof(short));
     test.value=0x0102;
     if((test.union_bytes[0]==1) &&(test.union_bytes[1]==2))
     {
          printf("big endian,高位字节存储在低地址,低位字节在高地址。\n");
     }else if((test.union_bytes[0] ==2 ) && (test.union_bytes[1]==1))
     {
        printf("little endian ,低位字节存放在内存的低地址,高位字节存放在内存的高地址 \n");
     }
     else
         printf("UNKOWN\n");
    // return 0;
}

   int main()
   {
       byteorder();
       system("pause");
       return 0;
   }


测试机器大小端_第1张图片

=================================

后来读 http://www.adobe.com/cn/devnet/rtmp.html 的http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/rtmp/pdf/rtmp_specification_1.0.pdf时候:

All integer fields are carried in network byte order, byte zero is
 the first byte shown, and bit zero is the most significant bit in a
 word or field
. This byte order is commonly known as big-endian. The
 transmission order is described in detail in Internet Protocol
 [RFC0791]. Unless otherwise noted, numeric constants in this
 document are in decimal (base 10).


可见,无论大端小端,向内存存入数据,都是先内存低地址,然后高地址。

这样,大端存入的时候,是先把高位数据存入(也是从低地址开始存),那么低位地址就对应了高位数据,所以叫做大端了。

你可能感兴趣的:(c/c++)