[饭后甜点] 大小端

作者: Quasars | 来源:发表于2016-07-13 14:32 被阅读52次

    网络/存储字节序中有大、小端的概念,简而言之就是内存地址由高到低与数据由高位到低位的对应关系.
    PS:哪个是大端,哪个是小端我也经常搞混.

    简单的测试程序一枚.(Updated/linux 3.16)

    
      1 #include <cstdio>                                                                                                                                                                         
      2 #include <string>                                                               
      3 using namespace std;                                                            
      4 #define THIS_PLATFORM __BYTE_ORDER == __LITTLE_ENDIAN                           
      5                                                                                 
      6 int main()                                                                      
      7 {                                                                               
      8                                                                                 
      9     const char* ptr = "test";                                                   
     10     printf("const var 's addr  %p\n", ptr);                                     
     11     unsigned int y = 0x12345678;                                                
     12     string x = "1234567890";                                                    
     13 #define FORstr(i,var) for(string::iterator i=var.begin(); i != var.end(); i++)  
     14                                                                                 
     15 #define FORint(i,var) for(char* i = (char*)&(var); i < (char*)(&(var) + 1); i++)
     16     //printf("%d\n", sizeof(x.data()));                                         
     17     printf("the string obj's addr is %p\n", &x);                                
     18                                                                                 
     19     FORstr(i, x){                                                               
     20         printf("%p %c\n", i, *i);                                               
     21     }                                                                           
     22                                                                                 
     23     printf("----\n");                                                           
     24     for(int i = 0; i < x.size(); i++){                                          
     25         printf("%p %c\n", &x[i], x[i]);                                         
     26     }                                                                           
     27                                                                                 
     28                                                                                 
     29     printf("-------\n");                                                        
     30     printf("THIS PLATFORM is %s.\n", THIS_PLATFORM == true ? "little_end" : "big_end");
     31     FORint(i, y){                                                               
     32         printf("%p %x\n", i, *i);                                               
     33     }                                                                           
     34                                                                                 
     35     return 0;                                                                   
     36 }                                                                                                                                         
    

    Output -

    const var 's addr  0x400d48
    the string obj's addr is 0x7fff448287e0
    0x7f5028 1
    0x7f5029 2
    0x7f502a 3
    0x7f502b 4
    0x7f502c 5
    0x7f502d 6
    0x7f502e 7
    0x7f502f 8
    0x7f5030 9
    0x7f5031 0
    ----
    0x7f5028 1
    0x7f5029 2
    0x7f502a 3
    0x7f502b 4
    0x7f502c 5
    0x7f502d 6
    0x7f502e 7
    0x7f502f 8
    0x7f5030 9
    0x7f5031 0
    -------
    THIS PLATFORM is little_end.
    0x7fff448287d8 78
    0x7fff448287d9 56
    0x7fff448287da 34
    0x7fff448287db 12
    

    Update--------------------
    来个判断大小端的代码吧?

      1 #include "common.h"                                                                                                                                                                                                                                                       
      2 #include <string.h>                                                             
      3                                                                                 
      4 bool isLittlendian()                                                            
      5 {                                                                               
      6     unsigned int x = 1;                                                         
      7     string s = "";                                                              
      8     s.resize(sizeof(x));                                                        
      9     memcpy(&s[0], &x, sizeof(x));                                               
     10     return s[0] == 1;                                                           
     11 }                                                                               
     12                                                                                 
     13 int main()                                                                      
     14 {                                                                               
     15     printf("this platform is %s\n", isLittlendian()? "littlendian" : "bigendian" );
     16     return 0;                                                                   
     17 }                                                                               
    

    相关文章

      网友评论

        本文标题:[饭后甜点] 大小端

        本文链接:https://www.haomeiwen.com/subject/yblrjttx.html