Go CString Turn char *, string

Source: Internet
Author: User

 

―――――――――――――――――――――

CString header file #include <afx.h>

String header file #include <string.h>

1. CString turn char *

CString CStr;

Char *p = (LPSTR) (LPCTSTR) CStr;

2. string Turn CString
Cstring.format ("%s", String.c_str ());

Using c_str () is really better than data () .

3. Char turn CString
Cstring.format ("%s", char*);

4. Char to string
string S (char *);

Can only be initialized, preferably in a place that is not initialized or with assign ().

5. string to char *
Char *p = STRING.C_STR ();

6. CString turn string
String s (Cstring.getbuffer ());

GetBuffer () must be releasebuffer (), otherwise the space occupied by the buffer will not be released .

―――――――――――――――――――――

in the C + + standard function library.
There are three functions to convert the contents of a string to a character array and c―string
1,data (), returns a string array without "/0"
2,c_str (), returns a string array with "/0"
3.copy ()

―――――――――――――――――――――

conversion between CString and int,char*,char[100] --

1. CString Mutual transfer int

(1) Convert characters to integers, you can use atoi,_atoi64, or atol.
(2) to convert a number to a CString variable, you can use the CString format function. Such as
CString s;
int i = 64;
S.format ("%d", i)
The Format function is very powerful and deserves your research.

Voidcstrdlg::onbutton1 ()
{
Todo:add your control Notificationhandler code here
CString
Ss= "1212.12″;
int Temp=atoi (ss);
CString AA;
Cal Format ("%d", temp);
AfxMessageBox ("var is" + AA);
}

Sart. Format ("%s", buf);

2. CString Mutual transfer char*

CString strtest;
char * CHARPOINT;

char * to CString
Charpoint= "Give string a value";
Strtest=charpoint;

CString to char *
Charpoint=strtest. GetBuffer (strtest. GetLength ());

no String,char*==char in standard C []==string

You can use the Cstring.format ("%s", char *) method to turn char * into CString. To turn CString into char*, use the operator (LPCSTR) toCString it.

3. CString conversion char[100]

CHAR[100] Tocstring

Char a[100];
CString Str ("aaaaaa");

CString to char[100]

strncpy (A, (LPCTSTR) str,sizeof (a));

Go CString Turn char *, string

Related Article

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: info-contact@alibabacloud.com 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.