字節序,終于搞得比較透了
#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;
}
#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 閱讀(403) 評論(0) 編輯 收藏 所屬分類: C&C++