VC data type conversion

xiaoxiao2021-03-06  104

This article will introduce some commonly used data types.

INT i = 100; long L = 200L; 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;

First, other data types are converted to string short integer (INT) ITOA (I, TEMP, 10); /// Transform 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, from other variables containing strings, pointers CString variables pointing to the string CString variable Str = "2008 Beijing Olympics"; BUF = (LPSTR) (LPCTSTSTR) Str; BSTR type _variant_t variable v1 = (_bstr_t) "program "" Buf = _com_util :: convertBSTRTOSTRING ((_ 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); four 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 can directly assign 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 Nea Before Using Head File Comutil.h, you can use char * p = _com_util :: ConvertBSTRTSTRING (B); Note: This function and namespace need comsupp.lib

Six, Variant, _variant_t and Colevariant Variant 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 correspondence between common data: unsigned char 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 IUnknown FAR * punkVal; VT_UNKNOWN IDispatch FAR * pdispVal; VT_DISPATCH SAFEARRAY FAR * parray; VT_ARRAY | * unsigned char 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_DISP ATCH SAFEARRAY FAR * FAR * PPARRAY; VT_ARRAY | * VARIANT FAR * PVARVAL; VT_BYREF | VT_VARIANT VOID FAR * BYREF; VT_BYREF _VARIANT_T is Variant's package class, which assigns the mandatory type conversion, and its constructor automatically handles these data types. For example: long l = 222; ion i = 100; _variant_t lval (l); lval = (long) i; use of ColeVariant Basic use with _variant_t, please refer to the following example: Colevariant V3 = "String", V4 = (long) 1999; cstring str = (bstr) v3.pbstrval; long i = v4.lval;

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

New Post(0)