CString, int, string, char * Conversion

Source: Internet
Author: User

1 CString, int, string, char * Conversion

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

// Convert CString to std: string

CString str = dlg. GetPathName (); setlocale (LC_ALL, "chs ");

Char * p = new char [256];

Wcstombs (p, str, 256 );

M_fileName = p;

1, string-> CString. format ("% s", string. c_str (); c_str () is indeed better than data (). 2, char-> string

String s (char *);

You can only initialize it. It is best to use assign () unless it is initialized (). 3, CString-> string s (CString. getBuffer (); after GetBuffer (), it must be ReleaseBuffer (); otherwise, no space occupied by the buffer will be 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 2, c_str () without "", returns a string array 3 with "", copy ()

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;

The s. Format ("% d", I) 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 ());

2 CString type conversion to int

CString type conversion to int

Converts a character to an integer. You can use atoi, _ atoi64, or atol.

// CString aaa = "16 ";

// Int int_chage = atoi (lpcstr) aaa );

To convert a number to a CString variable, you can use the Format function of CString. For example

CString s;

Int I = 64;

The s. Format ("% d", I) Format function is very powerful and worth your research.

If the char array is used, you can also use the sprintf function.

/Cstring ss = "1212.12 ";

// Int temp = atoi (ss );

// CString aa;

// Aa. Format ("% d", temp );

Numbers-> strings include CString: Format, FormatV, sprintf, and itoa without Afx.

3 char * In int

# Include <stdlib. h>

Int atoi (const char * nptr );

Long atol (const char * nptr );

Long atoll (const char * nptr );

Long atoq (const char * nptr );

4 CString, int, string, char * Conversion

String aa ("aaa ");

Char * c = aa. c_str ();

Cannot convert from 'const char * 'to 'Char *'

Const char * c = aa. c_str ();

5 conversion between CString, int, string, char * string. c_str () can only be converted to const char *,

To convert it to char *, write as follows:

String mngName;

Char t [200]; memset (t, 0,200); strcpy (t, mngName. c_str ());

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.