所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码.而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE.
1.将宽字符转换为多字符
如char是短字符,CString在Unicode下是宽字符
CString sPath = _T("we我");
//将第四个参数设为-1,即可返回所需的短字符数组空间的个数
DWORD dwNum = ::WideCharToMultiByte(CP_OEMCP,NULL,(LPCWSTR)sPath,-1,NULL,0,NULL,FALSE);//得到短字符数组空间的个数
char *psPath = NULL;
psPath = new char[dwNum];
if(!psPath)
{
delete []psPath;
psPath = NULL;
}
::WideCharToMultiByte (CP_OEMCP,NULL,(LPCWSTR)sPath,-1,psPath,dwNum,NULL,FALSE);
2.将短字符转换为款字符
char sText[20] = "WE我";
dwNum = ::MultiByteToWideChar (CP_ACP, 0, sText, -1, NULL, 0);
wchar_t *pwText;
pwText = new wchar_t[dwNum];
if(!pwText)
{
delete []pwText;
}
::MultiByteToWideChar(CP_ACP, 0, sText, -1, pwText, dwNum );
CString sText(pwText);
delete pwText;
pwText = NULL;
//
附:微软定义的几种字符指针
LPSTR: 一个32位指向字符串的指针,相当于char*
LPCSTR: 一个32位指向字符串常量的指针,相当于const char*
LPWSTR: 一个32位指向Unicode字符串的指针,相当于wchar_t*
LPCWSTR: 一个32位指向Unicode字符串常量指针,相当于const wchar_t*