我在VC6,mfc工程里定义
CString str=“abcd”;
CString str1=“我要出名!”;
CString str3=str+str1;
如果str3要是utf-8字符集,我要怎样转换呢,请给出详细说明,
不要只告诉我MultiByteToWideChar,再就是VC6默认是什么字符集,
小弟在网上搜了一些资料,可是还是不明白,望高人指点一下,谢谢!

解决方案 »

  1.   

    首先,那是不可能的。CString在一个工程里面要不uni要么mbcs。VC6的编辑器的默认字符集使用的是和你系统当前设置的一样。
    用MultiByteToWideChar或者c版本的,但是推荐MultiByteToWideChar。
      

  2.   

    先用MultiByteToWideChar将字符串转为Unicode,CodePage参数给CP_ACP;
    再用WideCharToMultiByte转为UTF-8,CodePage参数给CP_UTF8。
      

  3.   

    //////////////////////////////////////////////////////////////////////////
    // UTF转Unicode //
    //////////////////////////////////////////////////////////////////////////
    WCHAR*   UTF_8ToUnicode(char   *pText)
    {
            char   char_one;
    char   char_two;
    char   char_three;
    char   uchar[2];
    WCHAR   *unicode;char_one   =   pText[0];
    char_two   =   pText[1];
    char_three   =   pText[2];uchar[1]   =   ((char_one   &   0x0F)   < <   4)   +   ((char_two   > >   2)   &   0x0F);
    uchar[0]   =   ((char_two   &   0x03)   < <   6)   +   (char_three   &   0x3F);unicode   =   (WCHAR   *)uchar;
    return   unicode;
    }   
      

  4.   

    你既然用了CString str1="dadsa"这样的代码,那就说明你是多字节啦,现在你想转Unicode(utf-8字符集)是吧?
    先获取宽字节的长度,用DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, str1, -1, NULL, 0);接着申请空间:wchar_t *pwText;
      pwText = new wchar_t[dwNum];
      if(!pwText)
      {
       delete []pwText;
      }最后放进去MultiByteToWideChar (CP_ACP, 0, str1, -1, sText, dwSize);
    vc6默认是ansi字符集/多字节的方式。
    还有什么问题一并提出来好了
      

  5.   

    根据这个我给个代码CString CFileIndex::GBToUTF8(CString cstr)
    {
    CString result;
    WCHAR *strSrc;
    TCHAR *szRes;
    const char* str = (LPCTSTR)cstr;

    //获得临时变量的大小
    int i = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
    strSrc = new WCHAR[i+1];
    MultiByteToWideChar(CP_ACP, 0, str, -1, strSrc, i);

    //获得临时变量的大小
    i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
    szRes = new TCHAR[i+1];
    int j=WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, szRes, i, NULL, NULL);

    result = szRes;
    delete []strSrc;
    delete []szRes;

    return result;
    }返回的就是UTF8的了
      

  6.   

    这么多代码,想把楼主累死啊?
    A2W这个宏最方便
    #include <atlconv.h>
    Fun(A2W(szText));