字符編碼
兩種字符類型 char / wchar_t
TCHAR是一個宏
多字節編碼時:替換爲char
Unicode編碼時:替換爲wchar_t
l 不能使用strcpy這樣的ANSI C字符串函數處理wchar_t字符串,須使用wcs前綴的函數。
l 爲了使編譯器識別Unicode字符串,在字符串前面加L前綴。
wchar_t *szTest = L”This is a Unicode string”;
l 使用TCHAR: 不應該使用ANSI的strxxx和Unicode的wcsxxx, 須使用Tchar.h中定義的_tcsxxx,並用_TEXT代替L。
l
函數
_tcslwr 將字符串轉化成小寫字母(VS2005+WinCE6.3)
lstrcpy 複製(VS2005+WinCE6.3)
Cstring 轉 TCHAR
TCHAR之間的複製
lstrcpy(TCHAR , TCHAR);
lstrcpy(TCHAR , CString);
CString str= _T("留VS");
printf("str.getlength()=%d/n",str.GetLength()); //Unicode3個字符 //not set 4個字節。//mult 4//not set ==mult??
int a;
a=wcslen(str);
printf("a=%d/n",a);//3
a=_tcslen(str);
printf("a=%d/n",a);//3
TCHAR tstr[10];
a=wcslen(tstr);printf("a=%d/n",a);//1
lstrcpy(tstr,str);
a=wcslen(tstr);printf("a=%d/n",a);//3
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、winbase.h
#define lstrcpyW wcscpy
#ifdef UNICODE
#define lstrcpy lstrcpyW
#else
#define lstrcpy lstrcpyA
#endif
、、、、、、、、、、、、、、、、、、、、、、、、、、、tchar.h
#define _tcscpy wcscpy
wsprintf 複製(VS2005+WinCE6.3)
TCHAR *CurrentPath;
TCHAR CurrentFullPath[MAX_PATH];
wsprintf(CurrentFullPath,TEXT("%ls//*.*"),CurrentPath);
winbase.h中:
#ifdef UNICODE
#define wsprintf wsprintfW
#else
#define wsprintf wsprintfA
#endif
wcslen 獲取字符串長度(環境VS2005 )
if(0==wcslen(CurrentPath)){};
wcslen(CString)在multi-byte/not set字符集編譯錯誤
cannot convert parameter 1 from 'CString' to 'const wchar_t *'
在Unicode字符集正確,返回字符數。同Cstring.GetLength();
#define _tcslen wcslen //(tchar.h)
_tcslen 在multi-byte/not set/Unicode均可以用,在Unicode下返回字符數,在multi-byte/not set下返回字節數。
以上結果在PC上得出,
wince6.3版本在multi-byte/not set 情況下報錯,只能用Unicode,應該與所有SDK有關。
WinCE6.3 +Unicode:
Cstring str=_T(“留VS”);
wcslen(str)==_tcslen(str)==str.GetLength()==3。
strlen(str):報錯,
cannot convert parameter 1 from 'CString' to 'const char *'
wmemcmp比較字符串 (VS2005+WinCE6.3)
if(wmemcmp(FolderName,_T("//網絡"),wcslen(FolderName))==0){// 等於零代表想同}
_wcsuper 字符串小寫換爲大寫(VS2005+WinCE6.3)
TCHAR *FileName;
//賦值
TCHAR *UpperFileName =_wcsupr(FileName);
字符集轉化函數
MultiByteToWideChar
WideCharToMultiByte
LCMapString (簡體與繁體轉化)
悲催的,腦袋僵的很,明明知道Unicode一個字符兩個字節,還一直想怎麼獲取一個CString 的字節數! 字符數*2!!!!弱弱弱