Transformation between string,cstring,char* and _c language

Source: Internet
Author: User
Tags arrays

The transformation between string,cstring,char*

int Turn CString:
Cstring.format ("%d", int);
...............................
String Turn CString
Cstring.format ("%s", String.c_str ());
Using C_str () is indeed better than data ().
.......................................
char* Turn CString
Cstring.format ("%s", char*);
CString strtest;
char * CHARPOINT;
Charpoint= "Give string a value";
Strtest=charpoint; Direct Pay Value
...................................................................
CString Turn int
CString ss= "1212.12";
int Temp=atoi (ss); Atoi _atoi64 or Atol

Converts a character to an integer, and you can use Atoi, _atoi64, or ATOL.
int int_chage = Atoi ((LPCSTR) SS);
Or:
CString str = "23";
UINT UINT;
SSCANF (str, "%d", uint);
..............................
string int
..............................
char* Turn int
#include <stdlib.h>

int atoi (const char *nptr);
Long Atol (const char *nptr);
Long Long Atoll (const char *nptr);
Long Long Atoq (const char *nptr);
...................................................................
CString Turn string
String s (Cstring.getbuffer ());
GetBuffer () must be releasebuffer (), otherwise there is no space to free the buffer.
..........................................
int Turn string
..........................................
char* Turn string
string S (char *);
Your can only initialize, in the place that does not initialize is best still use assign ().
...................................................................
CString Turn char *

Copy Code code as follows:

CString strtest= "WWWWTTTTTTT";
Charpoint=strtest. GetBuffer (strtest. GetLength ());
CString conversion char[100]
Char a[100];
CString Str ("aaaaaa");
strncpy (A, (LPCTSTR) str,sizeof (a));
CString str= "AAA";
char* ch;
Ch= (char*) (LPCTSTR) str;

..........................................
int Turn char *
There is a function in the Stdlib.h itoa ()
Usage of itoa:
Itoa (i,num,10);
I need to convert numbers into characters
A variable that saves characters after NUM conversion
10 The cardinality of the conversion number (in) 10 means to convert the number in 10. It can be 2,8,16, etc. you like the type of the system
Prototype: char *itoa (int value, char* string, int radix);
Instance:
Copy Code code as follows:

#include "Stdlib.h"
#include "stdio.h"
Main ()
{
int i=1234;
Char s[5];
Itoa (i,s,10);
printf ("%s", s);
GetChar ();
}

..........................................
String char *
Copy Code code as follows:

Char *p = STRING.C_STR ();

String AA ("AAA");
Char *c=aa.c_str ();
String Mngname;
Char t[200];
memset (t,0,200);
strcpy (T,mngname.c_str ());

...................................................................
There is no String,char *==char in standard C []==string
You can use the Cstring.format ("%s", char *) method to turn char * to CString. To turn the CString into char *, with a damn
CString (LPCSTR) is OK.
Cannot convert from ' const char * ' to ' char * '
const char *C=AA.C_STR ();
STRING.C_STR () can only be converted to const char *

CString header file #include <afx.h>
String header file #include <string.h>

CString Turn char *

CString CStr;

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

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

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

Char Turn string
string S (char *);

String char *
Char *p = STRING.C_STR ();

CString Turn string
String s (Cstring.getbuffer ());

1,string-> CString
Cstring.format ("%s", String.c_str ());
Using C_str () is indeed better than data ().
2,char-> String
string S (char *);
Can only be initialized, preferably with assign () where it is not initialized.
3,cstring-> String
String s (Cstring.getbuffer ());
GetBuffer () must be releasebuffer (), otherwise there is no space to free the buffer.

In the C + + standard library of functions.
There are three functions that can convert the contents of a string into character arrays and c―string
1.data (), returns an array of strings without "/0"
2,C_STR (), returns an array of strings with "/0"
3,copy ()

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

CString with int, char*, char[100]--

CString with int, char*, char[100]--

CString Cross int

Converts a character to an integer, and you can use Atoi, _atoi64, or ATOL.
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 worth studying.

void Cstrdlg::onbutton1 ()
{
Todo:add your control notification handler code here
CString
Ss= "1212.12″;
int Temp=atoi (ss);
CString AA;
Aa. Format ("%d", temp);
AfxMessageBox ("var is" + AA);
}

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

CString Mutual Turn char*

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

CString to char *

CString Turn char *

CString CStr;

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

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

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

Char Turn string
string S (char *);

String char *
Char *p = STRING.C_STR ();

CString Turn string
String s (Cstring.getbuffer ());

1,string-> CString
Cstring.format ("%s", String.c_str ());
Using C_str () is indeed better than data ().
2,char-> String
string S (char *);
Can only be initialized, preferably with assign () where it is not initialized.
3,cstring-> String
String s (Cstring.getbuffer ());
GetBuffer () must be releasebuffer (), otherwise there is no space to free the buffer.

In the C + + standard library of functions.
There are three functions that can convert the contents of a string into character arrays and c―string
1.data (), returns an array of strings without "/0"
2,C_STR (), returns an array of strings with "/0"
3,copy ()

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

CString with int, char*, char[100]--

CString with int, char*, char[100]--

CString Cross int

Converts a character to an integer, and you can use Atoi, _atoi64, or ATOL.
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 worth studying.

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

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

CString Mutual Turn char*

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

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

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

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

CString conversion char[100]

Char a[100];
CString Str ("aaaaaa");
strncpy (A, (LPCTSTR) str,sizeof (a));


Charpoint=strtest. GetBuffer (strtest. GetLength ());

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

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

CString conversion char[100]

Char a[100];
CString Str ("aaaaaa");
strncpy (A, (LPCTSTR) str,sizeof (a));

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.