// Unicode下CString转Multichar *
int UnicodeCStringToMultiChar(CString srcStr, char *destChar)
{
if (NULL == destChar)
return -1;
int ansiCount=WideCharToMultiByte(CP_ACP,0,srcStr,-1,NULL,0,NULL,NULL);
WideCharToMultiByte(CP_ACP,0,srcStr,-1,destChar,ansiCount,NULL,NULL);//转换字符
return ansiCount;
}
// Unicode下Multichar *转Unicode CString
CString MultiCharToUnicodeCString(const char *pChar)
{
if (NULL == pChar)
return _T("");
//将char数组转换为wchar_t数组
int charLen = strlen(pChar); //计算pChar所指向的字符串大小,以字节为单位,一个汉字占两个字节
int len = MultiByteToWideChar(CP_ACP, 0, pChar, charLen, NULL, 0); //计算多字节字符的大小,按字符计算
wchar_t *pWChar = new wchar_t[len + 1]; //为宽字节字符数申请空间,
MultiByteToWideChar(CP_ACP, 0, pChar, charLen, pWChar, len); //多字节编码转换成宽字节编码
pWChar[len] = '\0';
//将wchar_t数组转换为CString
CString rStr;
rStr.Append(pWChar);
delete[] pWChar;
return rStr;
}
// Unicode下将TCHAR转为char
void TcharToChar(const TCHAR *tchar, char *_char)
{
if(NULL == tchar || NULL == _char)
return;
int iLength;
//获取字节长度
iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL);
//将tchar值赋给_char
WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL);
}
Unicode下将char转TCHAR
void CharToTchar(const char *_char, TCHAR *tchar)
{
if(NULL == _char || NULL == tchar)
return;
int iLength;
iLength = MultiByteToWideChar (CP_ACP, 0, _char, strlen(_char) + 1, NULL, 0);
MultiByteToWideChar (CP_ACP, 0, _char, strlen(_char) + 1, tchar, iLength);
}