Type conversion in VC.

xiaoxiao2021-04-02  221

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);

Second, get a pointer to the string from other variables containing strings

CString variable str = "2008 Beijing Olympics"; BUF = (LPSTR) (LPCTSTR) Str; BSTR type _variant_t variable v1 = (_BSTR_T) "programmer"; buf = _com_util :: convertBSTRTSTRING ((_ bstr_t) v1);

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

Short integer (int) i = ATOI (TEMP); long integer (long) L = atol (TEMP); Double) D = ATOF (TEMP)

Fourth, other data types are converted to CString using the member function format using 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 be assigned directly STR = UserName;

Five, BSTR, _BSTR_T and CCOMBSTR

CCOMBSTR, _BSTR_T is package to BSTR, BSTR is a 32-bit pointer to a string. Char * Convert to BSTR can be like this: BSTR B = _com_util :: ConvertStringTobstr ("Data"); /// Use Need to add header files Comutil.h, you can use char * p = _com_util :: ConvertBSTRTOSTRING (B);

Sixth, 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; seven, 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); /// take 8 digits

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

New Post(0)