I haven't used C ++ for a long time. It's not very easy to do. Data type conversion is really not as convenient as C ......
From: Skynet
The conversion of common data types is as follows:
Int I = 100; Long L = 2001; Float F = 300.2; Double D = 12345.119; Char username [] = "Woman Cheng peijun "; Char temp [200]; Char * Buf; Cstring STR; _ Variant_t V1; _ Bstr_t V2; |
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); // convert in binary mode |
Long (long)
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 )"ProgramMember "; Buf = _ com_util: convertbstrtostring (_ bstr_t) V1 ); |
Iii. Convert strings to other data types
Short INTEGER (INT)
Long (long)
Floating Point (double)
4. convert other data types to cstring
Use the cstring member function Format for conversion. For example:
INTEGER (INT)
Float)
Data Types supported by cstring constructors, such as string pointers (char *), can be directly assigned values.
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, you can use
| Char * P = _ com_util: convertbstrtostring (B ); |
6. Variant, _ variant_t, and colevariant
For the Variant Structure, refer to the definition of the tagvariant struct in the header file vc98 \ include \ oaidl. h.
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 with void variantinit (variantarg far * pvarg); in essence, the VT is set to vt_empty, the following table lists the correspondence between VT and 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; vt_error Cy cyval; vt_cy 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 * ppunkval; vt_byref | vt_unknown Idispatch far * ppdispval; vt_byref | vt_dispatch Safearray far * pparray; vt_array | * Variant far * pvarval; vt_byref | vt_variant Void far * byref; vt_byref |
_ 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; Word lovalue = loword (lparam); // take 16 bits Word hivalue = hiword (lparam); // you can specify 16 characters in height. |
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); // get the 8-bit high |