Summary of conversions between CString, String, and char*

Source: Internet
Author: User

char* turn into CString

Ø can be directly constructed as follows:

char * p = "Test";

CString str (p);

Ø You can use the member function format as follows:

char* p = "Test";

CString str;

Str. Format ("%s", p);

char* Convert to string

Direct from C har* constructs a string to

                                              chat* sz= "CWSASD";

                                               string S1 (SZ);

string convert into char*

Char *TP = const_cast<char*> (S1.c_str ());

string convert into CString

CString str2;

Str2. Format (L "%s", S1.c_str ());

CString convert into char*

CString Str ("test");

int Len=str.getlength ();

Char *cstr=new char[len+1];


WideCharToMultiByte (CP_OEMCP, 0, str,-1, CStr, len, null, NULL);

CString Convert to String

Method: "First convert CString to char* and then construct string from char*"

CString t = CString (s[i]);

int Len=t.getlength ();

Char *cstr=new char[len+1];


WideCharToMultiByte (CP_OEMCP, 0, T,-1, CStr, len, null, NULL);

String S1 (CStr);

Reprinted from Http://

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.