Conversion between cstring, string, and char [] In MFC

Source: Internet
Author: User

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;
Sscanf (STR, "% d", uint );
..............................
String to int
..............................
Char * to 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 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 *
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 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:
# Include "stdlib. H"
# Include "stdio. H"
Main ()
{
Int I = 1234;
Char s [5];
ITOA (I, S, 10 );
Printf ("% s", S );
Getchar ();
}
........................................ ..
String to char *
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. To convert cstring to char *, 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 *

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.