Conversion between char *, string and cstring

Source: Internet
Author: User

 

1. Convert the cstring class to the char * (lpstr) Type

Method 1: use forced conversion. For example:
Cstring thestring ("this is a test ");
Lptstr lpsz = (lptstr) (lpctstr) thestring;
Method 2: Use strcpy. For example:

Cstring thestring ("this is a test ");
Lptstr lpsz = new tchar [thestring. getlength () + 1];
_ Tcscpy (lpsz, thestring );
Method 3: Use cstring: getbuffer. For example:

Cstring S (_ T ("this is a test "));
Lptstr P = S. getbuffer ();
// Add the code using P here

If (P! = NULL) * P = _ T ('\ 0 ');
S. releasebuffer ();
// Release immediately after use, so that other cstring member functions can be used.

Cstring STR = "abcdef ";
Char * pbuf = STR, getbuffer (0 );
Str. releasebuffer ();

2. Convert string to char *

String is one of the C ++ standard libraries, which encapsulates string operations.
Convert string to char *
There are three methods:
1. Data (), returns a string array without "\ 0"

For example:
String STR = "ABC ";
Char * P = Str. Data ();
2. c_str returns a string array with "\ 0"

For example, string STR = "gdfd ";
Char * P = Str. c_str ();
3 copy
For example
String STR = "hello ";
Char P [40];
Str. Copy (p, 5, 0); // here, 5 represents the number of characters to be copied, and 0 represents the copy position.
* (P + 5) = '\ 0'; // You must manually add the Terminator.
Cout <P;

Iii. String Conversion to other data types

Temp = "123456 ";
1) Short INTEGER (INT)
I = atoi (temp );
2) long integer (long)
L = atol (temp );
3) floating point (double)
D = atof (temp );
String s; D = atof (S. c_str ());
4) BSTR variable
BSTR bstrvalue =: sysallocstring (L "programmer ");
... // Complete the use of bstrvalue
Sysfreestring (bstrvalue );
5) ccombstr variable
Ccombstr variables can be directly assigned values.
Ccombstr bstrvar1 ("test ");
Ccombstr bstrvar2 (temp );
6) _ bstr_t variable
_ Bstr_t type variables can be directly assigned values
_ Bstr_t bstrvar1 ("test ");
_ Bstr_t bstrvar2 (temp );

Iv. Convert char * to string

If you want to put a char
Convert to string,
AvailableString S (char *); 

V. String
Convert cstring

Cstring. Format ("% s", String. c_str ());

Vi. Char
Convert cstring
Cstring. Format ("% s", char *);

VII. cstring-> string

String S (cstring. getbuffer ());
Releasebuffer () is required after getbuffer (). Otherwise, no space occupied by the buffer is released.

8. 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)

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.