首页 > 代码库 > mfc CString,string,char* 之间的转换

mfc CString,string,char* 之间的转换

知识点:
CString转char*,string
string转char*,CString
char* 转CString,string



一、CString转char*,string
    //字串转换测试
    CString CString1;
    std::string string1;
    CHAR*  char1=NULL;
    

    //1
    string1=CString1.GetBuffer();
    CString1.ReleaseBuffer();
    //2
    char1=CString1.GetBuffer();
    CString1.ReleaseBuffer();
二、string转char*,CString
    //1
    CString1=string1.data();
    //2
    char1=new CHAR[string1.length()+1];
    strcpy(char1,string1.data());

三、CHAR* 转CStringwstring
        CString1=char1;
             string1=char1;

    //// TODO: 在此添加控件通知处理程序代码
    //CString CString1="CString";
    //std::string string1="string";
    //CHAR * char1="char1";

    ////一、CString 字串转string ,char*
    //string1=CString1;
    //char1=CString1.GetBuffer();
    //CString1.ReleaseBuffer();//释放掉字串缓冲区

    ////二、string 转CString char*
    //string1="string1";
    //CString1=string1.data();
    ////char1=string1.data();
    //new char[string1.length()+1];
    //strcpy(char1,string1.data());
    ////三、char* 转CString ,string
    //char1="char1";
    //CString1=char1;
    //string1=char1;

    //// TODO: 在此添加控件通知处理程序代码
    //CString CString1=L"CString";
    //std::wstring string1=L"string";
    //WCHAR * char1=L"char1";

    ////一、CString 字串转string ,char*
    //string1=CString1;
    //char1=CString1.GetBuffer();
    //CString1.ReleaseBuffer();//释放掉字串缓冲区

    ////二、string 转CString char*
    //string1=L"string1";
    //CString1=string1.data();
    ////char1=string1.data();
    //new WCHAR[string1.length()+1];
    //wcscpy(char1,string1.data());
    ////三、char* 转CString ,string
    //char1=L"char1";
    //CString1=char1;
    //string1=char1;
 
    // TODO: 在此添加控件通知处理程序代码
    CString CString1=  _T("CString");
    std::string string1=_T("string");
    TCHAR * char1=_T("char1");

    //一、CString 字串转string ,char*
    string1=CString1;
    char1=CString1.GetBuffer();
    CString1.ReleaseBuffer();//释放掉字串缓冲区

    //二、string 转CString char*
    string1=_T("string1");
    CString1=string1.data();
    //char1=string1.data();
    new TCHAR[string1.length()+1];
    strcpy(char1,string1.data());
    //三、char* 转CString ,string
    char1=_T("char1");
    CString1=char1;
    string1=char1;

 

mfc CString,string,char* 之间的转换