Visual C ++. String Conversion Method in NET [2]

xiaoxiao2021-03-06  57

V. BSTR, CHAR * and CSTRING conversion

(1) Char * Convert to CString

If char * is converted into CString, in addition to direct assignment, CString :: Format can be used. E.g:

Char charray [] = "this is a test"; char * p = "this is a test";

or

LPSTR P = "this is a test";

Or in the UNICODE that has been defined

TCHAR * P = _t ("this is a test");

or

LPTSTR P = _T ("this is a test"); cstring theString = Charray; stay.format (_T ("% s"), charray; TheString = P;

(2) CSTRING is converted into char *

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

Method 1. Use forced conversion. E.g:

CString TheString ("this is a test"); lptstr lpsz = (lpctstr) theString;

Method 2, using strcpy. E.g:

CString TheString ("this is a test"); lptstr lpsz = new tchar [theString.getLength () 1]; _ TCSCPY (LPSZ, THSSTRING);

It should be noted that the second parameter of STRCPY (or Disabled Unicode / MBCS) is const wchar_t * (unicode) or const char * (ANSI), and the system compiler will automatically convert it.

Method 3, using cstring :: getBuffer. E.g:

CSTRING S ("this is a test"); lptstr p = s.getBuffer (); // Add the code IF (P! = Null) * p = _t ('/ 0') here S.ReleaseBuffer (); // Release after using it to use other CString member functions

(3) BSTR is converted into char *

Method 1, using ConvertBSTRTSTRING. E.g:

#include #pragma comment (lib, "comsupp.lib") int _tmain (int argc, _TCHAR * argv []) {BSTR bstrText = :: SysAllocString (L "Test"); char * lpszText2 = _com_util :: ConvertBSTRToString (bstrText ); Sysfreestring (bstrtext); // completely release delete [] lpsztext2; return 0;}

Method 2, using _BSTR_T's assignment operator overload. E.g:

_BSTR_T B = BSTRTEXT; char * lpsztext2 = B;

(4) CHAR * Convert to BSTR

Method 1. Use the API functions such as SysallocString. E.g:

BSTR bstrtext = :: sysallocstring (l "test"); bstr bstrtext = :: sysallocstringlen (l "test", 4); bstr bstrtext = :: sysallocstringBytelen ("test", 4); method 2, use Colevariant or _variant_t . E.g:

// Colevariant Strvar ("this is a test"); _ variant_t strvar; bstr bstrtext = strvar.bstrval;

Method 3, using _bstr_t, this is the easiest way. E.g:

BSTR BSTRTEXT = _BSTR_T ("This Is A Test");

Method 4 uses CCOMBSTR. E.g:

BSTR BSTRTEXT = CCOMBSTR ("This Is A Test");

or

CCOMBSTR BSTR ("this is a test"); bstr bstrtext = BSTR.M_STR;

Method 5 uses ConvertStringTOBSTR. E.g:

Char * lpsztext = "test"; bstr bstrtext = _com_util :: ConvertStringTOBSTR (LPSZTEXT);

(5) CString Convert to BSTR

Usually achieved by using CStringt :: Allocsystring. E.g:

CString Str ("this is a test"); bstr bstrtext = str.allocsystring (); ... sysfreestring (bstrtext); // is released

(6) BSTR is converted into cstring

Generally, according to the following methods:

BSTR bstrtext = :: sysallocstring (l "test"); cstringa str; str.empty (); str = bstrtext;

or

CSTRINGA STR (BSTRTEXT);

(7) Conversion between ANSI, Unicode and Wide characters

Method 1. Use multibyTetowideCha to convert the ANSI characters to Unicode characters and use WideChartomultibyte to convert Unicode characters to an ANSI character.

Method 2, use "_t" to convert the ANSI to "General" type string, use "L" to convert ANSI to Unicode, and String an ANSI string is converted to string * objects in a managed C environment. E.g:

TCHAR TSTR [] = _T ("this is a test"); wchar_t wszstr [] = l "this is a test"; string * str = s "this is a test";

Method 3, using ATL 7.0 conversion macro and classes. ATL7.0 improves and adds many string conversion macros in the original 3.0 and provides the corresponding classes, which have a unified form shown in Figure 3:

Where the first C represents "class" to facilitate the difference between the ATL 3.0 macro, the second C represents the constant, 2 indicates "to", and the EX indicates to open up a certain size buffer. SourceType and DestinationType can be A, T, W and OLE, which are ANSI, Unicode, "General" types and OLE strings, respectively. For example, CA2CT is a string of ANSI to convert ANSI to a general type. Here are some sample code: lptstr TSTR = Ca2Tex <16> ("this is a test"); lpctstr tcstr = Ca2CT ("this is a test"); wchar_t wszstr [] = l "this is a test"; char * CHSTR = CW2A (WSZSTR);

Sixth, conclusion

Almost all procedures are used to use strings, while Visual C . NET is more frequent due to powerful, and the conversion between strings is more frequent. This article is almost involved in all current conversion methods. Of course, for the .NET framework, the CONVERT and TEXT classes can be used to perform mutual conversion between characters and character encoding.

转载请注明原文地址:https://www.9cbs.com/read-54173.html

New Post(0)