要在程序中实现 utf-8 到 UNICODE 的转换。找了一段代码,有一句调不通,望高手指教。
谢谢。  上代码CString CTestXmlDlg::UTF8ToString(const char *i_pszSource) 
{
if(NULL == i_pszSource) 
return ""; 
CString csTemp = ""; 
WCHAR *pwstrGroupName = NULL; 
try 

if(strlen(i_pszSource) == 0) 
return ""; 
int iBufferLength = strlen(i_pszSource) * 2; pwstrGroupName = new WCHAR[iBufferLength]; 
memset(pwstrGroupName, 0, sizeof(WCHAR) * iBufferLength); 
int iConverted = LdapUTF8ToUnicode(i_pszSource, strlen(i_pszSource), pwstrGroupName, sizeof(WCHAR) * iBufferLength); 
if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) 

// AfxMessageBox("缓冲区太小。"); 
delete []pwstrGroupName; 
return ""; 

char *pszTemp = _com_util::ConvertBSTRToString(pwstrGroupName);      //有问题??????!!!! 
csTemp = pszTemp; 
delete pszTemp; 

catch(CMemoryException *pe) 

// AfxMessageBox("内存错误,UTF8字符串无法转换为Unicode字符串。"); 
pe->ReportError(); 

catch(...) 

// AfxMessageBox("UTF8字符串无法转换为Unicode字符串。"); 
// AfxMessageBox(i_pszSource); 

if(pwstrGroupName) 
delete []pwstrGroupName; 
return csTemp;
}

解决方案 »

  1.   

    ConvertBSTRToString
    函数是把BSTR转换成char*类型的,你的pwstrGroupName是个WCHAR类型的不是BSTR的
    我看它从你出错后的代码的意思好像是把Unicode->ANSI的,其实char *pszTemp = _com_util::ConvertBSTRToString(pwstrGroupName);      //有问题??????!!!! 
    里面pwstrGroupName已经是Unicode了
      

  2.   

     void utf8ToUnicode(const string& src, wstring& result)
     {
      int n = MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, NULL, 0 );
      
      result.resize(n);
      
      ::MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, (TCHAR*)result.c_str(), result.length());
     } http://blog.csdn.net/ctl_2008_11/archive/2009/01/08/3734816.aspx