VC common data type use conversion detailed explanation

xiaoxiao2021-03-06  118

Reader level: beginner

Friends who have just contacted VCs often confuse the conversion of many data types, this article will introduce some common data types.

Let's first define some common type variables to explain

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;

First, other data types are converted to strings

Short integer (INT) ITOA (I, TEMP, 10); /// Transforms i into a string into TEMP, the last digit represents decimal ITOA (I, TEMP, 2); // Press binary to convert Long integer (LONG) LTOA (L, Temp, 10); Float, Double) can be converted with FCVT, which is an example in MSDN: int Decimal, Sign; Char * Buffer; Double Source = 3.1415926535; buffer = _FCVT (Source, 7, & Decimal, & Sign); Run Results: Source: 3.1415926535 Buffer: '31415927' DECIMAL: '31415927' DECIMAL: '31415927' DECIMAL: 1 SIGN: 0DECIMAL represents the position of the decimal point, SIGN represents symbol: 0 is positive, 1 is negative number cstring variable Str = "2008 Beijing Olympic Games"; buf = (LPSTR) (LPCTSTR) str; BSTR variable BSTR bstrValue = :: SysAllocString (L "programmer"); char * buf = _com_util :: ConvertBSTRToString (bstrValue); SysFreeString (bstrValue); AfxMessageBox (BUF); delete (buf); ccombstr BSTRVAR ("test"); char * buf = _com_util :: convertBSTRTOSTRING (BSTRVAR.M_STR); AFXMESSAGEBOX (BUF); delete (buf);

_BSTR_T variable _BSTR_T type is packaged for BSTR, because it has been overloaded = operator, so it is easy to use _bstr_t bstrvar ("test"); const char * buf = bstrvar; /// Do not modify the contents of BUF AFXMessageBox (BUF);

General Method (for non-COM data type) with 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);

Second, the string is converted to other data type STRCPY (TEMP, "123");

Short integer (int) i = ATOI (TEMP); long integer (long) L = atol (TEMP); dounce) D = ATOF (TEMP); cstring variable cstring name = Temp; BSTR variable BSTR BSTRVALUE = :: SysallocString (L "programmer"); ... // Complete using sysfreestring (BStrValue); CCOMBSTR variable CCOMBSTR type variable can directly assign CCOMBSTR BSTRVAR1 ("Test"); ccombstr BStrvar2 (TEMP);

_BSTR_T variable _BSTR_T type variable can be directly assigned _BSTR_T BSTRVAR1 ("Test"); _BSTR_T BSTRVAR2 (TEMP);

Third, other data types are converted to CString using the member function Format using the CString, for example:

Integer (int) str.format ("% d", i); floating point number (FLOAT) Str.Format ("% f", i); string pointer (char *), etc. has been supported by the CString constructor You can directly assign Str = username; for the data type not supported by Format, you can transfer to Char * through the method described above with respect to other data types to the char *, and then assign the value to the CString variable.

Fourth, BSTR, _BSTR_T and CCOMBSTR

CCOMBSTR is the package of ATL to BSTR, _BSTR_T is C to BSTR package, BSTR is a 32-bit pointer, but does not directly point to buffers of strings. CHAR * Convert to BSTR can be like this: BSTR B = _COM_UTIL :: ConvertStringTOBSTR ("Data"); /// Use Nea Before Using Comutil.h and Comsupp.libsysFreString (BSTRVALUE); vice versa Can use char * p = _com_util :: ConvertBSTRTSTRING (B); DELETE P; specifically refer to the specific description of the second paragraph.

CCOMBSTR is overloaded with _BSTR_t, which can be done directly =,! =, ==, etc., so it is very convenient to use. Especially _bstr_t, it is recommended to use it.

Five, Variant, _variant_t and Colevariant

Variant's structure can refer to the definition of struct tagvariant in the header file VC98 / include / OAIDL.H. For the assignment of the variant variable: first assign a value to the VT member, specify the data type, and then assign a value of the same data type in the federation, take an example: Variant Va; int a = 2001; va.vt = vt_i4; // indicate Integer data va.lval = a; // / assignment

For Variant that doesn't assign values, it is best to use Void Variantinit (Variantarg Far * Pvarg); in initialization, the essence is set to vt_empty, the following table, the following table, the following table, the following table, the corresponding relationship of VT and common data:

Byte bval; // vt_ui1. Short iv; // vt_i4. Float fltval; // vt_r4. Double dblval; // vt_r8. Variant_bool boolval; // vt_bool. Scode scode; // vt_error. Cyode scode; // vt_error. CY cyVal; // VT_CY DATE 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 * plval; // vt_byref | vt_i4. float far * pfltval; // vt_byref . | VT_R4 double FAR * pdblVal;. // VT_BYREF | 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 * FAR * ppunkVal;.. // VT_BYREF | VT_UNKNOWN IDispatch FAR * FAR * ppdispVal;. // VT_BYREF | VT_DISPATCH SAFEARRAY FAR * FAR *. PPARRAY; // vt_Array | *. Variant Far * pvarval; // vt_byref | vt_variant. Void far * byref; // generic Byref. CHAR CVAL; // vt_i1. Unsigned short uiVal; // vt_ui2. Unsigned long; int; // 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 a Variant's package class, which can be used to convert these data types using forced type conversion. To use #include , for example: long L = 222; ing i = 100; _variant_t lval (l); lval = (long) i;

Colevariant's use is basically the same as _variant_t, please refer to the following example: ColeVariant V3 = "String", V4 = (long) 1999; CString Str = (BSTR) v3.pbstrval; long; long i = v4.lval; six, Other COM data types

CLSIDHRESULT CLSID, LPCLSID PCLSID; CLSID CLSID (L "Mapi.Folder", & clsid; CLSID CLSID; CLSIDFROGID; CLSIDFROGID;

The CLSID obtained ProgIDWINOLEAPI ProgIDFromCLSID (REFCLSID clsid, LPOLESTR * lplpszProgID); for example, we have defined CLSID_IApplication, the following code obtained ProgIDLPOLESTR pProgID = 0; ProgIDFromCLSID (CLSID_IApplication, & pProgID); ... /// can pProgID CoTaskMemFree (pProgID) ; // Don't forget to release

7. ANSI and UnicodeUnicode are called a wide character string, and the use of Unicode strings in COM.

Convert ANSI to Unicode (1) to achieve this 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 clsid; long lLen = MultiByteToWideChar (CP_ACP, 0, szProgID, strlen (szProgID), szWideProgID, sizeof (szWideProgID)); szWideProgID [lLen] = '/ 0'; (3) through A2W macro implemented, for example: USES_CONVERSION; CLSIDFromProgID (A2W (szProgID), & clsid); converting Unicode to ANSI (1) using the WideCharToMultiByte, for example: Suppose // have a Unicode string wszSomeString ... char szANSIString [MAX_PATH]; WideCharToMultiByte ( CP_ACP, WC_compositeCheck, WSZSomString, -1, Szanstring, SizeOf (SzanSIString), NULL, NULL), (2) Using W2A macro to implement, for example: uses_conversion; ptemp = W2A (wszsomeString); eight, other

In the processing of messages, we often need to decompose 32-bit data (DWORD) such as WPARAM or LPARAM to two 16-bit data (Word), for example: lparam lparam; Word Lovalue = Loword (LPARAM); /// Take 16 Word HiValue = HiWord (LPARAM); /// Take 16

For 16-bit data (Word), we can decompose to two 8-bit data (byte) with the same method, for example: Word Wvalue; Byte Lovalue = LobyTe (WVALUE); /// Take low 8 bYte HiValue = Hibyte (wvalue); /// take 8 digits

Two 16-bit data (Word) synthesis 32-bit data (DWORD, LRESULT, LPARAM, or WPARAM) long makelong (Word Wlow, Word WLOW, Word Wlow, Word Wire); LParam MakelParam (Word Wlow, Word Word Word ); LRESULT MAKELRESULT (Word Wlow, Word WHigh); Two 8-bit data (Byte) synthesized 16-bit data (Byte Blow, Byte Bhigh);

From R (RED), G (Green), B (Blue) three colors Get colorref RGB (byte Byred, Byte Bygreen, Byte Byblue), Byte Byblue,, for example, ColorRef Bkcolor = RGB (0x22, 0x98, 0x34);

From the color type color value to RGB three color values ​​Byte Red = getRvalue (BKCOLOR); // get red color Byte green = getGvalue (bkcolor); // get green color Byte blue = getBvalue (bkcolor); // / Get the blue color

Nine, precautions If you need to use the ConvertBSTRTRING such function, you need to add the header file comutil.h, and add comsupp.lib in Setting, or directly below #Pragma Comment (lib, "comsupp.lib")

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

New Post(0)