Common data type use conversion detailed explanation

xiaoxiao2021-03-06  69

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; 1, 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 right BSTR's package, because it has been overloaded = operator, so it is easy to use _BSTR_T BSTRVAR ("Test"); const char * buf = bstrvar; /// Do not modify the content AfxMessageBox in BUF; general method ( Transfer char buffer with Sprintf for non-COM data types;

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 Variables of variable _BSTR_T type can be directly assigned _BSTR_T BSTRVAR1 ("Test"); _BSTR_T BSTRVAR2 (TEMP); three, other data types to convert to CSTRING using CString member function Format to convert, 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 does not immediately assign, it is best to use Void Variantinit (Variantarg Far * Pvarg); in the initialization, the nature is set to vt_empty, the following table We list VT Corresponding relationship with 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 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 far * 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 BYR Ef.char cval; // vt_i1.unsigned short uival; // vt_ui2.unsigned long; // 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 Yes. Variant's package, its assignment can use forced type conversion, and its constructor automatically handles these data types. In use, you need to add #include , for example: long L = 222; ion i = 100; _variant_t lval (l); lval = (long) i; use of ColeVariant Basis, please refer to the method of _variant_t method As example: Colevariant V3 = "String", V4 = (long) 1999; CString Str = (BSTR) v3.pbstrval; long i = v4.lval; six, other COM data types

The obtained ProgID CLSIDHRESULT CLSIDFromProgID (LPCOLESTR lpszProgID, LPCLSID pclsid); CLSID clsid; CLSIDFromProgID (L "MAPI.Folder", & clsid); CLSID give ProgIDWINOLEAPI ProgIDFromCLSID (REFCLSID clsid, LPOLESTR * lplpszProgID) according to; for example, we have defined CLSID_IApplication, below Code gets progidlpolestr pprogid = 0; progidFromclsid (CLSID_IApplication, & PProgID); ... // can use pProgid cotaskmemfree (PPROGID); // Don't forget to release seven, ANSI and UnicodeUnicode are called a wide character string, using COM All is a Unicode string.

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, wszsomeString, -1, szanstring, sizeof (szanstring), null, null; (2) Using W2A macro, for example: uses_conversion; ptemp = w2a (wszsomstring);

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 a 16-bit data for 16-bit data (Word) we can use the same method to decompose to two 8-bit data (byte), for example: Word Wvalue; Byte Lovalue = Lobyte (wvalue); /// Take a low 8 bYTE HIVALUE = Hibyte (wvalue); /// Repeat 8 bits of 16-bit data (Word) synthesis 32-bit data (DWORD, LRESULT, LPARAM, or WPARAM) Long Makelong (Word Wlow, Word Word); Word Wlow, Word Word; Lresult MakelResult (Word Wlow, Word Wree); Two 8-bit data (Byte) synthesis 16 bits Data (Word) Word MakeWord (Byte Blow, Byte Bhigh); Color Value of Colorref Type Color COLORREF RGB (Byte Byred, Byte Byhead, Byte Byred, Byte Byte Byred, Byte Byte Byblue) For example, ColorRef Bkcolor = RGB (0x22, 0x98, 0x34); from the color value of the 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 Lan Color Nine, Precautions If you need to use the ConvertBSTRTSTRING such function, you need to add the header file Comutil.h, and add comsupp.lib or directly #Pragma comment (lib, "comsupp.lib")

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

New Post(0)