从ANSIC到UNICODE比较简单
可以用CString的Format函数或默认转换:
char *pAnsicString = "Some test string";
CString strUnicode = pAnsicString;
还有MultiByteToWideChar函数。
从UNICODE到ANSIC也不难:
一
wcstombs
char* GetAnsicString(const CString &s)
{
int nSize = (s.GetLength() + 1) * sizeof(TCHAR);
char *pAnsicString = new char[nSize];
ZeroMemory(pAnsicString, nSize);
wcstombs(pAnsicString, s, nSize);
return pAnsicString;
}
CString strUnicode = _T("Some test string");
char *pAnsicString = GetAnsicString(strUnicode);
二
WideCharToMultiByte
WideCharToMultiByte是一个很强大的函数,它有很多参数。它允许你控制很多,象代码页、默认字符集等等。下面是一个例子:
char* GetAnsicString(const CString &s, UINT nCodePage)
{
int nSize = s.GetLength();
char *pAnsicString = new char[(nSize + 1) * sizeof(TCHAR)];
ZeroMemory(pAnsicString, [(nSize + 1) * sizeof(TCHAR));
WideCharToMultiByte(nCodePage, 0, s, nSize, pAnsicString, (nSize + 1) * sizeof(TCHAR), NULL, NULL);
return pAnsicString;
}
CString strUnicode = _T("Some test string");
char *pAnsicString = GetAnsicString(strUnicode, CP_ACP);
可以用CString的Format函数或默认转换:
char *pAnsicString = "Some test string";
CString strUnicode = pAnsicString;
还有MultiByteToWideChar函数。
从UNICODE到ANSIC也不难:
一
wcstombs
char* GetAnsicString(const CString &s)
{
int nSize = (s.GetLength() + 1) * sizeof(TCHAR);
char *pAnsicString = new char[nSize];
ZeroMemory(pAnsicString, nSize);
wcstombs(pAnsicString, s, nSize);
return pAnsicString;
}
CString strUnicode = _T("Some test string");
char *pAnsicString = GetAnsicString(strUnicode);
二
WideCharToMultiByte
WideCharToMultiByte是一个很强大的函数,它有很多参数。它允许你控制很多,象代码页、默认字符集等等。下面是一个例子:
char* GetAnsicString(const CString &s, UINT nCodePage)
{
int nSize = s.GetLength();
char *pAnsicString = new char[(nSize + 1) * sizeof(TCHAR)];
ZeroMemory(pAnsicString, [(nSize + 1) * sizeof(TCHAR));
WideCharToMultiByte(nCodePage, 0, s, nSize, pAnsicString,
return pAnsicString;
}
CString strUnicode = _T("Some test string");
char *pAnsicString = GetAnsicString(strUnicode, CP_ACP);