遇到了一个关于高地位字节序的问题,发现一般的c++笔试题上见到的不多,所以也研究下:

 1 /*
 2 *    写出下面的输出
 3 *
 4 */
 5 
 6 char cA[]={0x11,0x12,0x13,0x14,0x21,0x22,0x23,0x24};
 7 char cB[]={0x14,0x13,0x12,0x11,0x24,0x23,0x22,0x21};
 8 
 9 struct st
10 {
11     int a;
12     int b;
13 };
14 
15 void main()
16 {
17     st sa,sb;
18     st * s1 = &sa;
19     
20     s1 = (st*)cA;
21     cout << s1->a << endl;//336794129    //注意此处字节序 实际为 0x14,0x13,0x12,0x11
22     cout << s1->b << endl;//606282273     //实际为 0x24,0x23,0x22,0x21
23 
24 
25     st * s2 = &sb;
26     s2 = (st*)cB;
27     cout << s2->a << endl;//286397204   //注意此处字节序 实际为 0x11,0x12,0x13,0x14
28     cout << s2->b << endl;//555885348   //实际为 0x21,0x22,0x23,0x24
29 
30     system("pause");
31 
32 }

还有一个就是关于两个数组查找索引的笔试题:

  1 /*
  2 *    从cA内查找cB中的元素然后输出
  3 *    其中cB内的0xcc可以等于cA内的任何一个数
  4 *    其中cB内的最后一个数等于cA内的其中一个数加 1 类似“0x88”那就对应的是“0x89”
  5 */
  6 
  7 
  8 bool Func(unsigned char*,int,unsigned char*,int);//无符号的可以找到0xcc
  9 
 10 bool Func(char*,int,char*,int);//有符号的找不到0xcc //而又为什么我把0xcc换成0x10 就能找到呐?
 11 
 12 
 13 //???
 14 //其中有一个地方是:为什么我使用if(*(cB+2) == 0xcc ) 的时候 总是找不到 (如果数组cB是有符号数组)
 15 //如果我把cB类型换成无符号的就可以找到0xcc
 16 
 17 void main()
 18 {
 19     unsigned char cA[] = {0x80,0x34,0x55,0x73,0x56,0x45,0x32};
 20     unsigned char cB[] = {0x73,0x56,0xcc,0x33};
 21 
 22     char cE[] = {0x80,0x34,0x55,0x73,0x56,0x45,0x32};
 23     char cF[] = {0x73,0x56,0xcc,0x33};
 24     //char cF[] = {0x73,0x56,0x10,0x33};//0xcc 换成0x10 可以找到
 25     //char cF[] = {0x73,0x56,0x80,0x33};//0xcc 换成0x80 超过127 就会又找不到0x80
 26 
 27     char cC = 0xcc;
 28     int alen = sizeof(cA);
 29     int blen = sizeof(cB);
 30 
 31     int elen = sizeof(cE);
 32     int flen = sizeof(cF);
 33 
 34     bool bc = Func(cA,alen,cB,blen);    //使用无符号类型
 35 
 36     bool nc = Func(cE,elen,cF,flen);    //使用有符号类型
 37 
 38     if (bc)
 39     {
 40         cout<< "ok"<<endl;
 41     }else
 42     {
 43         cout << "no find" << endl;
 44     }
 45 
 46     system("pause");
 47 
 48 }
 49 
 50 bool Func(unsigned char* sc,int asize,unsigned char* dc,int bsize)
 51 {
 52     for (int i = 0;i<bsize;i++)
 53     {
 54         for (int j = 0;j<asize;j++)
 55         {
 56             if (*(dc+i) == 0xcc)
 57             {
 58                 cout<<"Find 0xcc = " << j<<endl;
 59                 continue;
 60             }else if (i == (bsize-1))
 61             {
 62                 if (*(dc+i) == (*(sc+j))+1)
 63                 {
 64                     cout << "find last item = " << j << endl;
 65                     continue;
 66                 }
 67             }else
 68             {
 69                 if (*(dc+i) == *(sc+j))
 70                 {
 71                     cout << "find other = " << j << endl;
 72                 }
 73 
 74             }
 75         }
 76     }
 77     return true;
 78 }
 79 
 80 bool Func(char* sc,int asize,char* dc,int bsize)
 81 {
 82     for (int i = 0;i<bsize;i++)
 83     {
 84         for (int j = 0;j<asize;j++)
 85         {
 86             
 87             if (*(dc+i) == 0xcc)    //同样的查找方式找不到0xcc
 88             //if (*(dc+i) == 0x10) //同样的查找可以找到0x10
 89             //if (*(dc+i) == 0x80) //同样的查找如果有符号超过127 就还是找不到0x80
 90             {
 91                 cout<<"Find 0xcc = " << j<<endl;
 92                 //cout<<"Find 0x10 = " << j<<endl;
 93                 //cout<<"Find 0x80 = " << j<<endl;
 94                 continue;
 95             }else if (i == (bsize-1))
 96             {
 97                 if (*(dc+i) == (*(sc+j))+1)
 98                 {
 99                     cout << "find last item = " << j << endl;
100                     continue;
101                 }
102             }else
103             {
104                 if (*(dc+i) == *(sc+j))
105                 {
106                     cout << "find other = " << j << endl;
107                 }
108 
109             }
110         }
111     }
112     return true;
113 
114 }

上面如果是有符号的类型的话,如果使用 临时变量 类似 char cTemp = 0xcc; 的话在if()语句中直接 *(dc+i) == cTemp; 同样也可以找到0xcc的 。c++研究的不深,望知道的给指点一下了。
=========================================================

又是苦逼的在找工作中。。。

打个小广告:c/c++ 北京 3年开发经验 合适的话求内推荐求带走,谢谢大牛们啦~~~(可私信)

posted on 2013-03-13 22:47  瓦楞球  阅读(625)  评论(0编辑  收藏  举报