Those who are new to VC programming are often confused about the conversion of many data types. This article will introduce the use of some common data types. We first define some common type variables to illustrate int I = 100; long l = 2001; float F = 300.2; double D = 12345.119; char username [] = "Cheng peijun "; char temp [200]; char * Buf; cstring STR; _ variant_t V1; _ bstr_t V2; 1. convert other data types to string short INTEGER (INT) ITOA (I, temp, 10); // convert I to a string and put it into temp. the last digit indicates the decimal ITOA (I, temp, 2 ); /// convert long integer (long) ltoa (L, temp, 10) in binary mode; float (double) can be converted using fcvt. 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: '000000' decimal: 1 sign: 0decimal indicates the decimal point position, sign indicates the symbol: 0 is a positive number, 1 is a negative cstring variable STR = "2008 Beijing Olympics"; Buf = (lpstr) (lpctstr) STR; BSTR variable BSTR bstrvalue = :: sysallocstring (L "programmer"); char * Buf = _ com_util: convertbstrtostring (bstrvalue); sysfreestring (bstrvalue); afxmessagebox (BUF); Delete (BUF ); ccombstr variable ccombstr bstrvar ("test"); char * Buf = _ com_ut Il: convertbstrtostring (bstrvar. m_str); afxmessagebox (BUF); Delete (BUF); _ bstr_t Variable _ bstr_t type is encapsulation of BSTR because the = operator has been overloaded, therefore, it is easy to use _ bstr_t bstrvar ("test"); const char * Buf = bstrvar; // do not modify the Buf content afxmessagebox (BUF ); general methods (for non-com data types) Use sprintf to convert 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); 2. String Conversion to another data type strcpy (temp, "123"); short INTEGER (INT) I = atoi (temp ); long Integer (long) L = atol (temp); floating point (double) d = atof (temp); cstring variable cstring name = temp; BSTR variable BSTR bstrvalue = :: sysallocstring (L "programmer ");... /// complete the use of bstrvalue sysfreestring (bstrvalue); ccombstr variable ccombstr type variables can be directly assigned ccombstr bstrvar1 ("test"); ccombstr bstrvar2 (temp ); _ bstr_t Variable _ bstr_t type variables can be directly assigned _ bstr_t bstrvar1 ("test"); _ bstr_t bstrvar2 (temp ); 3. convert other data types to cstring using the cstring member function format, for example, INTEGER (INT) Str. format ("% d", I); float Str. format ("% F", I); string pointer (char *) and other data types supported by the cstring constructor can be directly assigned STR = username; for data types not supported by format, you can convert the data type to char * by using the method described above, and then assign the value to the cstring variable. Iv. BSTR, _ bstr_t and ccombstrccombstr are the BSTR encapsulation of ATL. _ bstr_t is the encapsulation of BSTR by C ++. BSTR is a 32-bit pointer, but does not directly point to the string buffer. Char * can be converted to BSTR like this: bstr B = _ com_util: convertstringtobstr ("data"); // comutil needs to be added before use. H and comsupp. libsysfreestring (bstrvalue); otherwise, you can use char * P = _ com_util: convertbstrtostring (B); Delete P; For details, see section 1 and section 2. Ccombstr and _ bstr_t overload a large number of operators. You can directly perform = ,! =, = And so on, so it is very convenient to use. Especially _ bstr_t. We recommend that you use it. V. For the structure of variant, _ variant_t, and colevariantvariant, refer to the definition of struct tagvariant in vc98/include/oaidl. h In the header file. Assign a value to the variant variable: assign a value to the VT member to specify the data type, and then assign a value to the variable of the same data type in the union structure. For example: variant Va; int A = 2001; va. vt = vt_i4; // specifies the VA of the integer data. lval = A; // For variant that is not immediately assigned a value, it is best to initialize it with void variantinit (variantarg far * pvarg). In essence, the VT is set to vt_empty, the following table lists the correspondence between VT and common data: byte bval; // vt_ui1. short ival; // vt_i2. long lval; // vt_i4. float fltval; // vt_r4. double dblval; // vt_r8. variant_bool boolval; // vt_bool. scode; // vt_error. cy cyval; // Vt_cy. date; // vt_date. BSTR bstrval; // vt_bstr. decimal far * pdecval // vt_byref | vt_decimal. iunknown far * punkval; // vt_unknown. idispatch far * pdispval; // vt_dispatch. safearray far * parray; // vt_array | *. byte far * pbval; // vt_byref | vt_ui1. short far * pival; // vt_byref | vt_i2. long far * plval; // vt_byref | vt_i4. float far * pfltval; // vt_byref | vt_r4. double far * pdblval; // vt_ B Yref | vt_r8. variant_bool far * pboolval; // vt_byref | vt_bool. scode far * pscode; // vt_byref | vt_error. cy far * pcyval; // vt_byref | vt_cy. date far * pdate; // vt_byref | vt_date. BSTR far * pbstrval; // vt_byref | vt_bstr. iunknown far * ppunkval; // vt_byref | vt_unknown. idispatch far * ppdispval; // vt_byref | vt_dispatch. safearray far * pparray; // vt_array | *. variant far * pvarval; // vt_byr EF | vt_variant. void far * byref; // generic byref. char cval; // vt_i1. unsigned short uival; // vt_ui2. unsigned long ulval; // vt_ui4. int intval; // vt_int. unsigned int uintval; // vt_uint. char far * pcval; // vt_byref | vt_i1. unsigned short far * puival; // vt_byref | vt_ui2. unsigned long far * pulval; // vt_byref | vt_ui4. int far * pintval; // vt_byref | vt_int. unsigned int far * puintval; // VT _ Byref | vt_uint. _ variant_t is the encapsulation class of variant. The value assignment can be forced type conversion, and its constructor will automatically process the data types. # Include <comdef. h> for example, long l = 222; ing I = 100; _ variant_t lval (l); lval = (long) I; the use of colevariant is basically the same as that of _ variant_t, see the following example: colevariant V3 = "string", V4 = (long) 1999; cstring STR = (BSTR) v3.pbstrval; long I = v4.lval; 6. Other COM data types obtain clsidhresult clsidfromprogid (lpcolestr lpszprogid, lpclsid pclsid) According to progid; CLSID; clsidfromprogid (L "mapi. folder ", & CLSID); obtain progidwinoleapi progidfromclsid (refcl Sid CLSID, lpolestr * Secret); for example, we have defined clsid_iapplication. The following code gets progidlpolestr pprogid = 0; progidfromclsid (clsid_iapplication, & pprogid );... /// you can use pprogid cotaskmemfree (pprogid); // do not forget to release seven. ANSI and unicodeunicode are called wide character strings, and all Unicode strings are used in COM. Converts ANSI to Unicode (1) using the L macro. For example, clsidfromprogid (L "mapi. folder ", & CLSID); (2) Implement conversion through the multibytetowidechar function, for example, char * szprogid =" mapi. folder "; wchar szwideprogid [128]; CLSID; long llen = substring (cp_acp, 0, szprogid, strlen (szprogid), szwideprogid, sizeof (szwideprogid )); szwideprogid [llen] = '/0'; (3) implemented through the a2w macro, for example, uses_conversion; clsidfromprogid (a2w (szprogid), & CLSID ); convert Unicode to ANSI (1) Using widechartomultibyte. For example: // assume that a unicode string wszsomestring is already in use... char szansistring [max_path]; widechartomultibyte (cp_acp, signature, wszsomestring,-1, szansistring, sizeof (szansistring), null, null); (2) use the w2a macro, for example: uses_conversion; ptemp = w2a (wszsomestring); 8. In other message processing processes, we often need to use 32-bit data (DWORD) such as wparam and lparam) it is divided into two 16-bit data (Word), for example: lparam; Word lovalue = loword (lparam); // take the 16-bit low word hivalue = hiword (lparam ); /// take the high 16-bit data (Word) for 16-bit data, we can use the same method to break down into two high and low 8-bit data (byte), for example: Word wvalue; byte lovalue = lobyte (wvalue); // get low 8-bit byte hivalue = hibyte (wvalue); // get high 8-bit 16-bit data (word) synthesize 32-bit data (DWORD, lresult, lparam, or wparam) Long makelong (word wlow, word whigh); wparam makewparam (word wlow, word whigh); lparam makelparam (word wlow, word whigh); lresult makelresult (word wlow, word whigh); two 8-bit data (byte) to synthesize 16-bit data (Word) Word makeword (byte blow, byte bhigh ); colorref RGB (byte byred, byte bygreen) is obtained from R (red), g (green), B (blue ); for example, colorref bkcolor = RGB (0x22,0x98,0X34); from colorref color value to RGB color value byte Red = getrvalue (bkcolor ); /// get the red color byte Green = getgvalue (bkcolor); // obtain the green color byte Blue = getbvalue (bkcolor ); /// obtain blue color 9. Note: If convertbstrtostring is required, add the header file comutil. h, and add comsupp to setting. or directly add # pragma comment (Lib, "comsupp. lib ")