so true

          心懷未來,開創未來!
          隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
          數據加載中……

          字節序,終于搞得比較透了

          #include <iostream>
          #include <string.h>
          #include <stdio.h>
          #include <string>
          #include <vector>
          #include <map>
          #include <set>

          using namespace std;

          int main(int argc, char* argv[])
          {
              const int i = 0x61626364;
              printf("%x\n", *(char*)&i); //這里顯示64,因為little endian的原因,little代表LSB,endian代表變量所存放的內存的起始端,即內存的低地址
              printf("%x\n", i);//61626364

              const char* pp = "abcd"; //這在內存里從低地址到高地址依次存的真的是abcd,而之前定義的int i,在內存里從低地址到高地址依次存的卻是dcba
              printf("%x\n", *(int*)pp);//64636261

              wchar_t a=L'我';//wchar_t其實就是unsigned long, 注意,這里必須加前綴L,加L代表該字符是UNICODE,否則編譯會報warning(gcc會認為這樣的寫法和'abc'一樣邪惡), 這里的賦值其實就相當于unsigned long a = 0x6211
              printf("%x\n", a);//6211
              printf("%x\n", *(char*)&a);//這里顯示11,因為我的UNICODE編碼是6211
              cout << sizeof(a) << endl; // 4

              //const char* pW = L"我"; 編譯會報錯,因該用const wchar_t* pW = L"我";

              const char* p = "我"; //這個char序列存放的依次是e6, 88, 91, 我的utf8編碼是e68891,這是因為當前使用的編輯器vi設定了utf8編碼
              cout << strlen(p) << endl; //3
              printf("%x++\n", p[0]); //e6
              printf("%x\n", *(int*)p);//最終顯示為9188e6, %x代表將一個int的數值以十六進制的形式顯示出來,由于little endian的原因,在提取一個int的時候,會從內存的開始連續取4個btype,并且將最后一個byte最為int的MSB(即LSB放在前內存前端,MSB放在內存后端)

              const char* p2 = "abcd";
              printf("%x\n", *(int*)p2); //dcba

              return 0;
          }

          posted on 2011-04-08 10:19 so true 閱讀(409) 評論(0)  編輯  收藏 所屬分類: C&C++

          主站蜘蛛池模板: 江安县| 吉林省| 庄浪县| 翁源县| 泗水县| 昆山市| 平利县| 孙吴县| 高雄县| 潜山县| 汝阳县| 惠水县| 乌拉特中旗| 梁河县| 达孜县| 吴川市| 招远市| 伊金霍洛旗| 共和县| 客服| 苏尼特左旗| 集安市| 基隆市| 汉寿县| 澳门| 蒲江县| 会理县| 项城市| 海安县| 沐川县| 阿鲁科尔沁旗| 奈曼旗| 汉寿县| 镇巴县| 达拉特旗| 达日县| 霍州市| 广东省| 信阳市| 泌阳县| 武胜县|