违规网络音乐黑名单:UNICODE和ASIC码互换

来源:百度文库 编辑:九乡新闻网 时间:2024/05/16 21:44:03

从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);