Conversion between string, CString, char *

Source: Internet
Author: User

Conversion between string, CString, char *

Int to CString:
CString. Format ("% d", int );
...............................
String to CString
CString. format ("% s", string. c_str ());
C_str () is indeed better than data.
.......................................
Char * To CString
CString. format ("% s", char *);
CString strtest;
Char * charpoint;
Charpoint = "give string a value ";
Strtest = charpoint; // pay the value directly
........................................ ...........................
CString to int
CString ss = "1212.12 ";
Int temp = atoi (ss); // atoi _ atoi64 or atol

Converts a character to an integer. You can use atoi, _ atoi64, or atol.
Int int_chage = atoi (lpcstr) ss );
Or:
CString str = "23 ";
UINT uint;
Sscanf (str, "% d", uint );
..............................
String to int
..............................
Char * to int
# Include <stdlib. h>

Int atoi (const char * nptr );
Long atol (const char * nptr );
Long atoll (const char * nptr );
Long atoq (const char * nptr );
........................................ ...........................
CString to string
String s (CString. GetBuffer ());
ReleaseBuffer () is required after GetBuffer (). Otherwise, no space occupied by the buffer is released.
........................................ ..
Int to string
........................................ ..
Char * to string
String s (char *);
You can only initialize it. It is best to use assign () unless it is initialized ().
........................................ ...........................
Convert CString to char *

Copy codeThe Code is 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 ());
CString str = "aaa ";
Char * ch;
Ch = (char *) (LPCTSTR) str;

........................................ ..
Convert int to char *
There is a function itoa () in stdlib. h ()
Itoa usage:
Itoa (I, num, 10 );
I number to be converted to characters
Variable for saving characters After num Conversion
10: The base (in hexadecimal) 10 of the number to be converted. It can also be 2, 8, 16, and so on.
Prototype: char * itoa (int value, char * string, int radix );
Instance:Copy codeThe Code is as follows: # include "stdlib. h"
# Include "stdio. h"
Main ()
{
Int I = 1234;
Char s [5];
Itoa (I, s, 10 );
Printf ("% s", s );
Getchar ();
}

........................................ ..
String to char *
Copy codeThe Code is 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 in Standard C, char * = char [] = string
You can use the CString. Format ("% s", char *) method to convert char * To CString. Convert CString to char *.
You can use the operator (LPCSTR) CString.
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>

Convert CString to char *

CString cstr;

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

String to CString
CString. format ("% s", string. c_str ());

Convert char to CString
CString. format ("% s", char *);

Char to string
String s (char *);

String to char *
Char * p = string. c_str ();

CString to string
String s (CString. GetBuffer ());

1, string-> CString
CString. format ("% s", string. c_str ());
C_str () is indeed better than data.
2, char-> string
String s (char *);
Only Initialization is allowed. It is best to use assign () instead of initialization ().
3, CString-> string
String s (CString. GetBuffer ());
ReleaseBuffer () is required after GetBuffer (). Otherwise, no space occupied by the buffer is released.

As mentioned in C ++ standard function library
There are three functions that can convert the content of a string to a character array and a C-string
1. data (), returns a string array without "/0"
2, c_str (), returns a string array with "/0"
3, copy ()

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

Conversion between CString, int, char *, and char [100 --

Conversion between CString, int, char *, and char [100 --

CString mutual int Conversion

Converts a character to an integer. You can use atoi, _ atoi64, or atol.
To convert a number to a CString variable, you can use the Format function of CString. For example
CString s;
Int I = 64;
S. Format ("% d", I)
The Format function is very powerful and worth your research.

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 );

Convert CString to char *

/// Char * TO cstring
CString strtest;
Char * charpoint;
Charpoint = "give string a value ";
Strtest = charpoint;

/// Cstring TO char *

Convert CString to char *

CString cstr;

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

String to CString
CString. format ("% s", string. c_str ());

Convert char to CString
CString. format ("% s", char *);

Char to string
String s (char *);

String to char *
Char * p = string. c_str ();

CString to string
String s (CString. GetBuffer ());

1, string-> CString
CString. format ("% s", string. c_str ());
C_str () is indeed better than data.
2, char-> string
String s (char *);
Only Initialization is allowed. It is best to use assign () instead of initialization ().
3, CString-> string
String s (CString. GetBuffer ());
ReleaseBuffer () is required after GetBuffer (). Otherwise, no space occupied by the buffer is released.

As mentioned in C ++ standard function library
There are three functions that can convert the content of a string to a character array and a C-string
1. data (), returns a string array without "/0"
2, c_str (), returns a string array with "/0"
3, copy ()

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

Conversion between CString, int, char *, and char [100 --

Conversion between CString, int, char *, and char [100 --

CString mutual int Conversion

Converts a character to an integer. You can use atoi, _ atoi64, or atol.
To convert a number to a CString variable, you can use the Format function of CString. For example
CString s;
Int I = 64;
S. Format ("% d", I)
The Format function is very powerful and worth your research.

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 );

Convert CString to 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 in Standard C, char * = char [] = string

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

CString conversion char [100]

Char a [100];
CString str ("aaaaaa ");
Strncpy (a, (LPCTSTR) str, sizeof ());

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

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

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

CString conversion char [100]

Char a [100];
CString str ("aaaaaa ");
Strncpy (a, (LPCTSTR) str, sizeof ());

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.