Summary of data type conversion

xiaoxiao2021-03-05  30

Description: This article is purely a post related to type conversion. I didn't test all methods, just to give you a reference.

Reader level: beginner 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 , 2); // ////-binary conversion (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: 1 SIGN: 0 DECIMAL Represents the position of the decimal point, SIGN represents 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 :: ConvertBSTRToString (bstrVar.m_str); AfxMessageBox (buf); Delete (BUF); _BSTR_T variable _bstr_t type is the package of BSTR, because it has been overloaded = operator, so it is easy to use _bstr_t bstrvar ("test"); const char * buf = bstrvar; // / not to modify BUF content AFXMessageBox (BUF); General Method (For non-COM data type) Complete 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, string conversion For other data types STRCPY (TEMP, "123"); short integer (int) i = ATOI (TEMP); long integer (long) L = atol (TEMP); Double) D = ATOF (TEMP) ; Cstring variable cstring name = Temp; BSTR variable BSTR BSTRVALUE = :: sysallocstring (l "programmer"); ... // Complete the use of Sysfreestring (BStrValue);

CCOMBSTR variable CCOMBSTR type variable can be directly assigned CCOMBSTR BSTRVAR1 ("Test"); _BSTR_T Variable_BSTR_T type variable can be directly assigned _BSTR_T BSTRVAR1 ("Test"); _BSTR_T BSTRVAR2 (TEMP); three, other Data Types Convert to CString Use CString Member Function Format to convert, for example: integer (int) str.format ("% d", i); floating point number (FLOAT) Str.Format ("% f", i); characters The data type that has been supported by the CString constructor can directly assign Str = username; the data type not supported for Format can be converted to the CHAR * above for the data type mentioned above. Go to Char * and assign the value to the CString variable. Fourth, BSTR, _BSTR_T and CCOMBSTR CCOMBSTR is ATL to the package of 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"); /// Using Comutil.h and ComSupp.lib sysfreestring (BSTRVALUE); vice versa Can use char * p = _com_util: : ConvertBSTRTSTRING (B); DELETE P; Specific paragraph 1, 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. V. Variant, _variant_t and Colevariant Variant's structure can refer to the definition of struct tagvariant in the header file VC98InCludeoAIDL.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 with common data: byte bval; // vt_ui1. Short iv; // 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. Cyber ​​* 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 ulval; // vt_ui4. Int IntVal; // vt_int. Unsigned int UintVal ; // vt_uint; // 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 whose assignment can use forced type conversion, and its constructor automatically handles these data types.

Plus #include, for example: long 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 type According to PROGID, CLSID HRESULT CLSIDFROMPROGID (LPCOLESTR LPSZPROGID, LPCLSID PCLSID) ; CLSID clsid; CLSIDFromProgID (L "MAPI.Folder", & clsid); obtained according to the CLSID ProgID WINOLEAPI ProgIDFromCLSID (REFCLSID clsid, LPOLESTR * lplpszProgID); for example, we have defined CLSID_IApplication, the following code obtained ProgID LPOLESTR pProgID = 0; ProgIDFromCLSID ( CLSID_IApplication, & PPROGID); ... // You can use pProgid CotaskMemfree (PPROGID); // Don't forget to release seven, ANSI and Unicode Unicode are called a wide character string, and the COM uses 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] = ''; (3) achieved by A2W macro, For example: USES_CONVERSION; CLSIDFromProgID (A2W (szProgID), & clsid); convert Unicode to ANSI (1) using 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 (wszsomeString); eight, other for messages, we often need Decomposing 32-bit data such as WPARAM or LPARAM into two 16-bit data (Word), for example: lParam LParam; Word Lovalue = loword (lparam); /// Take low 16-bit 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 Low 8 bYTE HIVALUE = Hibyte (WValue); /// Take 8 bits of 16-bit data (Word) synthesis 32-bit data (DWORD, LRESULT , LPARAM, or WPARAM) Long Makelong (Word Wlow, Word Word); Lresult Makelparam (Word Wlow, Word Wree); LRESULT MAKELRESULT (Word Wlow, Word WHIGH); Two 8-bit Data (Byte) Synthesis of 16-bit Data (Word) Word Makeword (Byte Blow, Byte Bhigh); Colorref RGB (Byte BiTe) from R (Red), G (Green), B (Blue). BYRED, BYTE BYGREEN, BYTE BYBLUE = Getgvalue (bkcolor); // gets green color Byte Blue = getBValue (BKColor);

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

New Post(0)