MFC C++ Cstring and string conversion

Reprinted from: https://www.cnblogs.com/HappyEDay/p/7016162.html

I tried a lot of methods to convert CString to string
, but it doesn't work, I use vs2010

------Solution--------------------
unicode:
CString sz1 = L"abc";
std::string sz2 = CT2A(sz1.GetBuffer()); //Convert to non-unicode.

Non-unicode:
CString sz1 = "abc";
std::string sz2 = sz1.GetBuffer();  

------Solution--------------------
Upstairs is the solution, and then provide a few methods under UNICODE

C/C++ code
//method one
CString theCStr;
std::string STDStr( CW2A( theCStr.GetString() ) );        //Method adopted
//Method Two
CString m_Name;
CT2CA pszName(m_Name);
std::string m_NameStd(pszName);
//method three
CString str = L"Test";
std::wstring ws(str);
std::string s;
s.assign(ws.begin(), ws.end());

CString/string difference and its conversion

When programming with MFC, the string we get from the dialog box using GetWindowText is of type CString, and CString is a class belonging to MFC. And some standard C/C++ library functions cannot directly operate on the CString type, so we often encounter the situation of converting the CString type to char* and other data types. Here's a summary of the memo here!
First of all, it should be clear that there is no string type in standard C. String is a class that extends string operations in standard C++. But we know that there is a header file string.h in standard C, and we need to distinguish clearly here, this string is not another string. The string.h header file defines some functions that we often use to manipulate strings, such as strcpy, strcat, strcmp, etc., but the operation objects of these functions are all strings pointed to by char*. The C++ string class operation object is a string type string. This class reloads some operators and adds some string operation member functions, making it more convenient to manipulate strings. Sometimes we want to use string strings and char* strings together, so the conversion of these two types will also be involved.

1. Conversion of CString and string
stringstr="ksarea";
CStringcstr(str.c_str());//or CString cstr(str.data()); only when initialized
cstr=str.c_str(); or cstr=str.data();
str =cstr.GetBuffer(0); //CString -> string
cstr.format("%s", str.c_str()); //string->CString
cstr.format("%s", str.data() ); //string->CString
str = LPCSTR(cstr); //The difference between CString->string
/*c_str() and data() is: the former returns a string with '/0', while the latter returns a string without '/0' string */
2. Conversion of CString and int
inti=123;
CStringstr;
str.format("%d",i);//int->CString other basic type conversions are similar to
i=atoi(str);//CString->int and (atof,atol)
3. Conversion of char* and CString
CStringcstr="ksarea";
char* ptemp=cstr.getbuffer(0);
char* str;
strcpy(str,ptemp);//CString->char*
cstr.releasebuffer(-1);

char*str="lovesha";
CStringcstr=str;//char*->CString string类型不能直接赋值给CString
至于int与float、string与char*之间的转化可以使用强制转化,或者标准库函数进行。对于CString与其他类型的转化方法很多,但其实都殊途同归,朝着一个方向即将类型首先转化为char*类型,因为char*是不同类型之间的桥梁。得到char*类型,转化为其他类型就非常容易了。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326769780&siteId=291194637