MultiByteToWideChar和WideCharToMultiByte两个转化函数
怎么在UNICODE工程里怎么把CString字符串转化?函数给的参数搞的我头都大了,就是函数的输入和输出都要CString
给出代码

解决方案 »

  1.   

    CString本来就是根据你工程是否unicode编码来自动生成是否支持unicdoe.//Unicode转换Ansi处理
    LPWSTR pwbBuffer=new WCHAR[255];
    PBYTE sourValue = new BYTE[255];        DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pwbBuffer,-1,NULL,0,NULL,FALSE);//计算要使用空间
            dwCount=dwNum;//unicode返回的值要是转换后的大小        if(!(sourValue = (BYTE *)malloc(dwNum)))
            {
                return false;
            }        WideCharToMultiByte (CP_OEMCP,NULL,pwbBuffer,-1,(PBYTE)sourValue,dwNum,NULL,FALSE);
      

  2.   

    USES_CONVERSION;
    CString str = L"abc";
    char* p = W2A(str);
      

  3.   


    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;
    }这是我读取两种不同格式的文件的代码,把GB的换成UTF-8的
      

  4.   


    CString m_FilePath;
    CW2A lpFilePath(m_FilePath);//lpFilePath现在是char*TCHAR *lptstr = m_FilePath.GetBuffer();//转换成TCHAR
      

  5.   

    人家MultiByteToWideChar参数不是CString啊,你凭什么一定要“输入和输出都要CString”呢?要这样的函数,自己开发啊,否则,就得按照规则办事。
      

  6.   

    恩我的是从两文件读的数据格式不一样,不是纯粹的CString
      

  7.   

    CString所储存的字符串是与项目所使用的字符集相同的,同一项目中不能即储存多字节字符串又储存Unicode字符串。在VS2005中,可以用CStringA类储存多字节字符串,用CStringW储存Unicode字符串。
      

  8.   

    我没有说是函数的参数一定要CString,只要是能转就成,但转我这里编译不过。
    哪位好新人能给出能编译的代码段来,环境是VS2005的UNICODE。
      

  9.   

    sffofn 朋友谢谢您给出代码,我是新手,报了一堆的错误,不知道为什么
      

  10.   


    CString cstr = _T("asdasd");
    const char* str = (LPCTSTR)cstr;
    //错误 1 error C2440: “初始化”: 无法从“LPCTSTR”转换为“const char *” d:\Works\我的网站\www.6692.com\Tools\C++\1\1Dlg.cpp 213

      

  11.   

    CString strText = _T("XXXXXXXXXX"); 
    LPWSTR lpwText = strText.GetBuffer(strText.GetLength()); 
    strText.ReleaseBuffer(); char buff[MAX_LENGTH]; 
    int iSizeS = wcslen(lpwText); 
    WideCharToMultiByte(CP_ACP, 0, lpwText, iSizeS, buff, sizeof(buff), NULL, NULL); // 将宽字节(UNICODE)转化成多字节(ANSI)的 
    buff[iSizeS] = '\0'; 你可以使用 
    MultiByteToWideChar(CP_ACP, 0, buff1, sizeof(buff1), wbuff, sizeof(wbuff)); 
    将多字节转化成宽字节。 
    有char 和wchar_t两种类型的缓冲区。
      

  12.   

    VS2005可以这样相互转换:
    CString strT = _T("字符串");
    CStringA strA = CT2A(strT);
    CStringW strW = CT2W(strT);
    strT = CA2T(strA);
    strT = CW2T(strW);
    strA = CW2A(strW);
    strW = CA2W(strA);
      

  13.   

    既然定义了/ _UNICODE,那么CString的对象就是UNICODE编码,把const char * str改成 const TCHAR* str就行,或者const wchar_t* str或者 const unsigned short* str;
    为了方便程序在编码上的转换,最好都用tchar.h文件里面的类型。
    还有lz的问题,没有道理,自己去看看cstring的构造函数去,不看mfc源码,真可怕。