1,Windows定义Unicode数据类型 WCHAR(Unicode字符) PWSTR(指向Unicode字符串的指针) PCWSTR(指向一个恒定的Unicode字符串的指针) 以上都是Unicode字符和字符串。同时windows定义了ANSI/Unicode通用数据类型PTSTR和PCTSTR。 Unicode宏没有前置下划线,_UNICODE宏用于c运行期头文件,而UNICODE用于windows头文件。
2,c对UNICODE的支持 比如常见函数:char *strchr(const char*,int) --------->wchar_t *wcschr(const wchar_t *,wchar_t); int strcmp(const char*,const char*)-------->int wcscmp(const wchar_t*,const wchar_t*); 例如strcpy--->wcscpy,strlen->wcslen;实际是把wcs取代ANSI前缀str。 3,TChar.h TChar.h文件唯一作用帮助创建ANSI和UNICODE通用源代码。 如果定义_UNICODE,TCHAR声明为: typedef wchar_t TCHAR 如果没有定义_UNICODE: typedef char TCHAR; 分配字符串 TCHAR szString[100];或者TCHAR *szE="hello world"; 如果生成UNICODE不是ANSI串,必须改写为:TCHAR *szE=L"hello world";(L意思是UNICODE字符串编译); 同时也可以改写为TCHAR *szE=_TEXT("hello world"); 4,Windows编程中建议使用系统函数,而不是c运行字符串函数 使用时候加上ShlApi.h头文件。函数样子为StrCat、StrChr等等(开头字母大写) 5,windows字符串函数 windows提供的UNICODE函数;例如: lstrcat,lstcmp,lstcmpi、lstcpy、lstrlen。这些函数可以调用UNICODE也可以调用ANSI版本。 如果没有定义了UNICODE,lstcat将扩展为lstcatA,如果定义了UNICODE,扩展为strcatW。
6,Unicode 和ANSI相互转化 char szA[100]; // An ANSI string buffer WCHAR szW[100];//A Unicode string buffer sprintf(szA,"%s","ANSI Str"); //Normal sprintf: all string are ANSI sprintf(szA,"%S",L"Unicode Str"); //Converts Unicode string to ANSI swprintf(szw,L"%S",L"Unicode Str");// Normal swprintf: all string are Unicode swprintf(szW,L"%S","ANSI Str"); // Converts ANSI string to Unicode (严格注意大小写S) LPTSTR 等价于 char * LPCSTR 等价于 const char * 7,UNICODE和ANSI之间转化字符(函数参考MSDN) MultiByteToWideChar (多字节字符串转化为宽字节字符串) WideCharToMultiBye(宽字符串转为多字节字符串)
实例:
char buf[MAX_PATH]; size_t aLen = strlen( buf ) + 1;
int wLen = MultiByteToWideChar(CP_ACP,0,buf,aLen,NULL,0);
LPWSTR lpw = new WCHAR [wLen]; MultiByteToWideChar(CP_ACP,0,buf,aLen,lpw,wLen); 。。。。。。。。。。。。//使用lpw delete []lpw;
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ameyume/archive/2009/08/16/4448398.aspx
|