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

Posted whzym111

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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* 之间的转换的主要内容,如果未能解决你的问题,请参考以下文章

MFC常见问题以及解决方法_Cstring和string互相转换

mfc中,如何将CString 转换成 string

MFC中char*,string和CString之间的转换(待补充)

MFC 中char转CString问题

MFC中输出string;

CString 在 MFC 中编辑控件