CStringUtf8ToUnicode

CString CStringUtf8ToUnicode( CString Utf8 )
{
	int wLen = 0;
	CString strUnicode;
	LPSTR pBufChar = NULL;
	LPWSTR pBufWchar  = NULL;
	
	//CString to char
	wLen = WideCharToMultiByte(CP_ACP, 0, Utf8, -1, NULL, 0, NULL, NULL);
	pBufChar = new char[wLen + 1];
	memset( pBufChar , 0, wLen + 1 );
	WideCharToMultiByte(CP_ACP, 0, Utf8, -1, pBufChar, wLen, NULL, NULL);
	
	//char to Unicode
	wLen = MultiByteToWideChar(CP_UTF8, 0, pBufChar, -1, NULL, NULL);
	pBufWchar = new wchar_t[wLen];
	memset(pBufWchar, 0, wLen * sizeof(wchar_t));
	MultiByteToWideChar(CP_UTF8, 0, pBufChar, -1, pBufWchar, wLen);

	strUnicode = CString(pBufWchar);
	if( pBufChar ) delete pBufChar;
	if( pBufWchar ) delete pBufWchar;

	return strUnicode;
}
posted @ 2014-08-25 22:24  1CM  阅读(315)  评论(0编辑  收藏  举报