Conversion of common VC Data Types

Source: Internet
Author: User
Conversion of common VC Data Types

 
Let's first define some common type variables to illustrate
 
Int I = 100;
Long L = 2001;
Float F = 300.2;
Double D = 12345.119;
Char username [] = "user ";
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)
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. For example:
 
INTEGER (INT)
Str. Format ("% d", I );
Float)
Str. Format ("% F", 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
 
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 ready *
Unsigned char far * pbval; vt_byref 1_vt _ ui1
Short far * pival; vt_byref 1_vt _ I2
Long far * plval; vt_byref 1_vt _ I4
Float far * pfltval; vt_byref 1_vt _ r4
Double far * pdblval; vt_byref 1_vt _ R8
Variant_bool far * pboolval; vt_byref 1_vt _ bool
Scode far * pscode; vt_byref implements VT _ Error
Cy far * pcyval; vt_byref 1_vt _ cy
Date far * pdate; vt_byref 1_vt _ date
BSTR far * pbstrval; vt_byref 1_vt _ BSTR
Iunknown far * ppunkval; vt_byref 1_vt _ Unknown
Idispatch far * ppdispval; vt_byref implements VT _ dispatch
Safearray far * pparray; vt_array limit *
Variant far * pvarval; vt_byref using 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
---------------------------------------------------------------
 
INT-> str itoa, atoi
Double-str ftoa, atof
 
_ Bstr_t, _ variant_t, cstring, long, etc:
Let me give you a detailed example.
First understand the constructor and operator = of the _ variant_t and _ bstr_t classes.
There are many reloads in it,
Other types assign values to _ variant_t:
_ Variant_t () Throw ();
_ Variant_t (const variant & varsrc) Throw (_ com_error );
_ Variant_t (const variant * pvarsrc) Throw (_ com_error );
_ Variant_t (const _ variant_t & var_t_src) Throw (_ com_error );
_ Variant_t (variant & varsrc, bool fcopy) Throw (_ com_error );
_ Variant_t (short SSRC, vartype vtsrc = vt_i2) Throw (_ com_error );
_ Variant_t (long lsrc, vartype vtsrc = vt_i4) Throw (_ com_error );
_ Variant_t (float fltsrc) Throw ();
_ Variant_t (double dblsrc, vartype vtsrc = vt_r8) Throw (_ com_error );
_ Variant_t (const Cy & cysrc) Throw ();
_ Variant_t (const _ bstr_t & bstrsrc) Throw (_ com_error );
_ Variant_t (const wchar_t * wstrsrc) Throw (_ com_error );
_ Variant_t (const char * strsrc) Throw (_ com_error );
_ Variant_t (bool bsrc) Throw ();
_ Variant_t (iunknown * piuknownsrc, bool faddref = true) Throw ();
_ Variant_t (idispatch * pdispsrc, bool faddref = true) Throw ();
_ Variant_t (const decimal & decsrc) Throw ();
_ Variant_t (byte bsrc) Throw ();
Operator = overload format:
_ Variant_t & operator = (const variant & varsrc) Throw (_ com_error );
_ Variant_t & operator = (const variant * pvarsrc) Throw (_ com_error );
_ Variant_t & operator = (const _ variant_t & var_t_src) Throw (_ com_error );
_ Variant_t & operator = (short SSRC) Throw (_ com_error );
_ Variant_t & operator = (long lsrc) Throw (_ com_error );
_ Variant_t & operator = (float fltsrc) Throw (_ com_error );
_ Variant_t & operator = (double dblsrc) Throw (_ com_error );
_ Variant_t & operator = (const Cy & cysrc) Throw (_ com_error );
_ Variant_t & operator = (const _ bstr_t & bstrsrc) Throw (_ com_error );
_ Variant_t & operator = (const wchar_t * wstrsrc) Throw (_ com_error );
_ Variant_t & operator = (const char * strsrc) Throw (_ com_error );
_ Variant_t & operator = (idispatch * pdispsrc) Throw (_ com_error );
_ Variant_t & operator = (bool bsrc) Throw (_ com_error );
_ Variant_t & operator = (iunknown * psrc) Throw (_ com_error );
_ Variant_t & operator = (const decimal & decsrc) Throw (_ com_error );
_ Variant_t & operator = (byte bsrc) Throw (_ com_error );
 
With the above two functions, for example:
Double F = 1, 1.0
_ Variant_t V;
V = f; // It is valid. You can see the operator = overload mode.
Cstring STR = "DDD"
_ Variant_t V;
V = Str. allocsysstring () or V = (_ bstr_t) (char *) STR;
You can.
 
_ Variant_t to another form
First, you must determine the form you want to convert.
Double F;
_ Variant_t v
F = V. dblval or F = (double) V;
 
Appendix: _ variant_t Operator
Operator short () const throw (_ com_error );
Operator long () const throw (_ com_error );
Operator float () const throw (_ com_error );
Operator double () const throw (_ com_error );
Operator cy () const throw (_ com_error );
Operator bool () const throw (_ com_error );
Operator decimal () const throw (_ com_error );
Operator byte () const throw (_ com_error );
Operator _ bstr_t () const throw (_ com_error );
Operator idispatch * () const throw (_ com_error );
Operator iunknown * () const throw (_ com_error );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.