1. convert other data types to strings
Short INTEGER (int)
Itoa (I, temp, 10); // convert I to a string and put it into temp. the last digit indicates decimal.
Itoa (I, temp, 2); // Binary Conversion
Long (long)
Ltoa (l, temp, 10 );
2. Obtain the pointer to the string from other variables containing the string
CString variable
Str = "2008 Beijing Olympics ";
Buf = (LPSTR) (LPCTSTR) str;
_ Variant_t variable of the BSTR type
V1 = (_ bstr_t) "programmer ";
Buf = _ com_util: ConvertBSTRToString (_ bstr_t) v1 );
Iii. Convert strings to other data types
Strcpy (temp, "123 ");
Short INTEGER (int)
I = atoi (temp );
Long (long)
L = atol (temp );
Floating Point (double)
D = atof (temp );
4. convert other data types to CString
Use the CString member function Format for conversion,
Example: INTEGER (int)
Str. Format ("% d", I );
Data Types supported by CString constructors, such as string pointers (char *), can be directly assigned values.
Str = username;
V. BSTR, _ bstr_t and CComBSTR
CComBSTR and _ bstr_t are encapsulation of BSTR, and BSTR is a 32-bit pointer to a string.
Char * can be converted to BSTR like this: BSTR B = _ com_util: ConvertStringToBSTR ("data"); // you need to add the header file comutil. h before use
Otherwise, use char * p = _ com_util: ConvertBSTRToString (B );
6. VARIANT, _ variant_t, and COleVariant
Assign a value to the VARIANT variable: assign a value to the vt member to specify the data type, and then assign a value to the variable of the same data type in the union structure, for example:
VARIANT va;
Int a = 2001;
Va. vt = VT_I4; // specify Integer Data
Va. lVal = a; // value assignment
For VARIANT that is not immediately assigned a value, it is best to initialize it with Void VariantInit (variantarg far * pvarg). The essence is to set vt to VT_EMPTY.
_ Variant_t is the encapsulation class of VARIANT, and its value assignment can be forced type conversion. Its constructor will automatically process these data types. For example:
Long l = 222;
Ing I = 100;
_ Variant_t lVal (l );
LVal = (long) I;
The use of COleVariant is basically the same as that of the _ variant_t method. See the following example:
COleVariant v3 = "string", v4 = (long) 1999;
CString str = (BSTR) v3.pbstrVal;
Long I = v4.lVal;
VII. Others
During message processing, we often need to split WPARAM, LPARAM, and other 32-bit data (DWORD) into two 16-bit data (WORD), for example:
LPARAM lParam;
WORD loValue = LOWORD (lParam); // take 16 bits
WORD hiValue = HIWORD (lParam); // The height is 16 bits.
For a 16-bit data (WORD), we can use the same method to break down the high and low 8-bit data (BYTE), for example:
WORD wValue;
BYTE loValue = LOBYTE (wValue); // get 8 bits
BYTE hiValue = HIBYTE (wValue); // The value is 8 bits in height.