C ++ type conversion (string)

Source: Internet
Author: User

Cstring is a special C ++ object, which contains three values: A pointer to a data buffer, a valid string in the buffer (which is inaccessible and is a hidden area located under the cstring address), and a buffer length. The valid characters can be any number between 0 and the maximum buffer length minus 1 (because the string ends with a null character ). Characters and buffer length are cleverly hidden.

(1) convert char * To cstring

If char * is converted to cstring, you can use cstring: format in addition to direct value assignment. For example:

Char charray [] = "char test ";
Tchar * P = _ T ("char test"); (or lptstr P = _ T ("char test ");)
Cstring thestring = charray;
Thestring. Format (_ T ("% s"), charray );
Thestring = P;

(2) convert cstring to char *

If the cstring type is converted to the char * (lpstr) type, the following three methods are often used:

Method 1: use forced conversion. For example:

Cstring thestring (_ T ("char test "));
Lptstr lpsz = (lptstr) (lpctstr) thestring;

Method 2: Use strcpy. For example:

Cstring thestring (_ T ("char test "));
Lptstr lpsz = new tchar [thestring. getlength () + 1];
_ Tcscpy (lpsz, thestring );

It should be noted that the second parameter of strcpy (or _ tcscpy of the value to be moved) is const wchar_t * (UNICODE) or const char * (ANSI ), the system compiler will automatically convert it.

Method 3: Use cstring: getbuffer.

If you need to modify the content in cstring, there is a special method that can be used, that is, getbuffer, which is used to return a writable buffer pointer. If you only want to modify characters or truncate strings, for example:
Cstring S (_ T ("char test "));
Lptstr P = S. getbuffer ();

Lptstr dot = strchr (p ,''.'');

// AddCode

If (P! = NULL)

* P = _ T ('');
S. releasebuffer (); // release immediately after use, so that other cstring member functions can be used.

In the range between getbuffer and releasebuffer, you must not use any method of the buffer cstring object you want to operate on. Because the integrity of the cstring object is not guaranteed before releasebuffer is called.

----------------------------------------------------

Convert other data types to strings

1. Short INTEGER (INT)
ITOA (I, temp, 10); // convert I to a string and put it into temp. the last digit indicates decimal.
ITOA (I, temp, 2); // convert in binary mode
2. Long (long)
Ltoa (L, temp, 10 );
3. Floating Point Number (float, double)
Fcvt can be used to complete the conversion. This is an example in msdn:
Int decimal, sign;
Char * buffer;
Double Source = 3.1415926535;
Buffer = _ fcvt (source, 7, & decimal, & sign );
Running result: Source: 3.1415926535 Buffer: '20180101' decimal: 1 sign: 0
Decimal indicates the decimal point position, sign indicates the symbol: 0 is a positive number, and 1 is a negative number.
4. Cstring variable
STR = "2008 Beijing Olympics ";
Buf = (lpstr) (lpctstr) STR;
5. BSTR variable
BSTR bstrvalue =: sysallocstring (L" Program Member ");
Char * Buf = _ com_util: convertbstrtostring (bstrvalue );
Sysfreestring (bstrvalue );
Afxmessagebox (BUF );
Delete (BUF );
6. Ccombstr variable
Ccombstr bstrvar ("test ");
Char * Buf = _ com_util: convertbstrtostring (bstrvar. m_str );
Afxmessagebox (BUF );
Delete (BUF );

7. _ Bstr_t variable
_ Bstr_t is the encapsulation of BSTR. It is easy to use because = operator has been overloaded.
_ Bstr_t bstrvar ("test ");
Const char * Buf = bstrvar; // do not modify the Buf content
Afxmessagebox (BUF );

8. General method (for non-com data types)
Use sprintf to complete the conversion
Char buffer [200];
Char c = '1 ';
Int I = 35;
Long J = 1000;
Float F = 1.7320534f;
Sprintf (buffer, "% C", C );
Sprintf (buffer, "% d", I );
Sprintf (buffer, "% d", J );
Sprintf (buffer, "% F", F );

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.