VC data type conversion

Source: Internet
Author: User

Those who are new to VC programming are often confused about the conversion of many data types. This article will introduce the use of some common 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 [] = "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)

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 | *

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

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.