VC common data type use conversion detailed explanation

xiaoxiao2021-03-06  41

VC usual data type Use to convert the detainer level: friends who have just contacted VC programming often confuse the conversion of many data types, this article

Will introduce some commonly used 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); /// converts i to strings in TEMP, last digital table

Ten Bao ITOA (I, TEMP, 2); // Press binary to convert long (LONG) LTOA (L, TEMP, 10); floating point number (FLOAT, DOUBLE) can be converted with FCVT, this is MSDN Example: int Decimal, Sign; Char * Buffer; Double Source = 3.1415926535; Buffer = _FCVT (Source, 7, & Decimal, & Sign); Run Results: Source: 3.1415926535 Buffer: '31415927' DECIMAL: 1

Sign: 0Decimal represents the position of the decimal point, Sign indicates the symbol: 0 is positive, 1 is negative number 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_util :: ConvertBSTRTSTRING (BSTRVAR.M_STR); AFXMessageBox (BUF); Delete (BUF);

_BSTR_T variable _bstr_t type is the package to BSTR, because it has been overloaded = operator, so it is easy

Use _bstr_t bstrvar ("test"); const char * buf = bstrvar; /// Do not modify the contents of the 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 can

Direct assignment str = username; for the data type not supported by Format, you can pass the above data above

The method of transformation to char * is first transferred to CHAR * and assigns 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

32-bit pointers, but not directly point to buffers of strings. Char * Convert to BSTR can be like this: BSTR B = _COM_UTIL :: ConvertStringTOBSTR ("Data"); // / / before use

Plus Comutil.h and Comsupp.libsysFreestring (BSTRVALUE); vibrantly 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 header file vc98 / include / oaidl.h

Tagvariant's definition. For the assignment of the variant variable: first assign a value to the VT member, specify the data type, then

The variable value of the same data type in the joint structure is assigned, and an example: Variant Va; Int a = 2001; VA.VT = VT_I4; // indicates integer data VA.LVAL = a; // value

For Variant that doesn't assign a value right, it is best to use Void Variantinit (Variantarg)

Far * pvarg; perform initialization, the essence is set to vt_empty, the following table

Correspondence to 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 assigns a mandatory type conversion,

The constructor will automatically process these data types. 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 i = v4.lval; six, other COM data types

Get CLSIDHRESULT CLSIDFROGID (LPCLSID) based on Progid

PCLSID; CLSID CLSID; CLSIDFROMPROGID (L "Mapi.Folder", & clsID;

Get progidwinoleapi progidfromclsid according to CLSID (Refclsid CLSID, LPOLESTR *

lplpszProgID); for example, we have defined CLSID_IApplication, the following code to get ProgIDLPOLESTR pProgID = 0; ProgIDFromCLSID (CLSID_IApplication, & pProgID); ... /// can use pProgID CoTaskMemFree (pProgID); // Do not 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) to achieve the conversion, for example by MultiByteToWideChar function: 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) achieved by A2W macro, for example: USES_CONVERSION; CLSIDFromProgID (A2W (szProgID), & clsid); converting Unicode to ANSI (1) Use widechartomultibyte, for example: // Suppose already has a Unicode string wszsomeString ... char szanstruing [max_path]; widechartomultibyte (CP_ACP, WC_CompositeCheck,

WszsomeString, -1, SzansString, Sizeof (Szanstring), NULL,

NULL); (2) Using W2A macro to achieve, for example: uses_conversion; ptemp = w2a (wszsomeString); eight, other

We often need 32-bit data such as WPARAM or LPARAM (DWORD) in the processing of the message.

Decompose into two 16-bit data (Word), for example: lParam LParam; Word Lovalue = Loword (LPARAM); /// Take a low 16-bit word HiValue = HiWord (lparam); /// Take 16 bits

For 16-bit data (Word) we can use the same method to decompose to two 8 digits

According to (byte), for example: Word Wvalue; Byte Lovalue = Lobyte (Wvalue); /// Take a low 8 bYTE HIVALUE = Hibyte (Wvalue); /// Take 8 bits

Two 16-bit data (Word) synthesized 32-bit data (DWORD, LRESULT, LPARAM, or

WPARAM) Long Makelong (Word Wlow, Word WHIGH); Word Wlow, Word Whot Word; Lresult MakelResult (Word Wlow, Word Word Word Word Word; Two 8-bit data (Byte ) Synthesis 16-bit data (Word) Word Makeword (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 ConvertBSTRTSTRING such function, you need to add header files.

Comutil.h and add comsupp.lib in Setting or directly #pragma

Comment (Lib, "Comsupp.lib")

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

New Post(0)