Common data type use conversion detailed explanation

zhaozj2021-02-11  156

Common data type conversion Detailed Author: Cheng Peijun

Reader level: Friends who have just contacted VC programming often feel confused for many data types, this article will introduce some common data types. We first define some common type variables to explain INT i = 100; long L = 2001; float f = 300.2; double d = 12345.119; char username [] = "Female Heroes 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); II, get pointers that point 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); three The string is converted to other data types STRCPY (TEMP, "123");

Short integer (int) i = ATOI (TEMP); long integer (long) L = atol (TEMP); Double DOUBLE D = ATOF (TEMP); four other data types to convert to cstring members using CString Function Format converts, 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 STR = UserName; 5, 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 file Comutil.h Avobilize can use char * p = _com_util :: convertbstertostring (b); six, Variant, _variant_t with 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 follows: Colevariant V3 = "String", V4 = (long) 1999; CString Str = (BSTR) V3.pbstrVal; long i = v4.lval; seven, other

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

New Post(0)