C ++ data type conversion

Source: Internet
Author: User
Reader level: Beginner

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;
CharUsername [] = "Cheng peijun ";
CharTemp [1, 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 );
Floating Point Number (float, double)
Fcvt can be used to complete the conversion. This is an example in msdn:
Int decimal, sign;
Char* Buffer;
Double Source = 3.1415926535;
Buffer =_Fcvt (source, 7, & decimal, & sign );
Running result: Source: 3.1415926535 Buffer: '20180101' decimal: 1 sign: 0
Decimal indicates the decimal point position, sign indicates the symbol: 0 is a positive number, and 1 is a negative number.
CstringVariable
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_The T type is encapsulation of BSTR, because the = operator has been overloaded, so it is easy to use
_BSTR_T bstrvar ("test ");
ConstChar* Buf = bstrvar; // do not modify the Buf content
Afxmessagebox (BUF );

General method (for non-com data types)
Use sprintf to complete the conversion
CharBuffer [200];
CharC = '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 );

Ii. 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 );
CstringVariable
CstringName = temp;
BSTR variable
BSTR bstrvalue =: sysallocstring (L "programmer ");
... // Complete the use of bstrvalue
Sysfreestring (bstrvalue );

Ccombstr variable
Ccombstr variables can be directly assigned values.
Ccombstr bstrvar1 ("test ");
Ccombstr bstrvar2 (temp );

_BSTR_T variable
_BSTR_T-type variables can be directly assigned values.
_BSTR_T bstrvar1 ("test ");
_BSTR_T bstrvar2 (temp );

Iii. convert other data typesCstring
UseCstringFor example:

INTEGER (INT)
Str. Format ("% d", I );
Float)
Str. Format ("% F", I );
String pointer (Char*) Has beenCstringData Types supported by constructors can be directly assigned values.
Str= Username;
For data types not supported by format, you can convert themChar* First goChar*, And then assign the valueCstringVariable.

Iv. BSTR,_BSTR_T and ccombstr

Ccombstr is the encapsulation of BSTR by ATL,_BSTR_T is the encapsulation of BSTR by C ++. BSTR is a 32-bit pointer, but it does not directly point to the buffer zone of the string.
Char* Conversion to BSTR can be as follows:
Bstr B =_Com_Util: convertstringtobstr ("data"); // comutil. h and comsupp. Lib must be added before use.
Sysfreestring (bstrvalue );
Otherwise, you can use
Char* P =_Com_Util: convertbstrtostring (B );
Delete P;
For details, refer to the specific descriptions in sections 1 and 2.

Ccombstr and_BSTR_T is overloaded with a large number of operators. You can directly perform = ,! =, = And so on, so it is very convenient to use.
Especially_BSTR_T. We recommend that you use it.

V. 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 first initialize it with void variantinit (variantarg far * pvarg); the essence is to set VT to vt_Empty. The following table lists the correspondence between VT and common data:

Byte 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.
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.
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; // generic byref.
CharCval; // vt_I1.
Unsigned short uival; // vt_Ui2.
Unsigned long ulval; // vt_Ui4.
Int intval; // vt_Int.
Unsigned int uintval; // vt_Uint.
CharFar * pcval; // 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 the encapsulation class of variant, and its value assignment can be forced type conversion. Its constructor will automatically process these data types.
# Include <comdef. h> must be added for use.
For example:
Long L = 222;
Ing I = 100;
_Variant_T lval (L );
Lval = (long) I;

Use of colevariant and_Variant_The T method is basically the same. See the following example:
Colevariant V3 = "string", V4 = (long) 1999;
Cstring Str= (BSTR) v3.pbstrval;
Long I = v4.lval;

6. Other COM Data Types

Obtain CLSID Based on progid
Hresult clsidfromprogid (lpcolestr lpszprogid, lpclsid pclsid );
CLSID;
Clsidfromprogid (L "mapi. folder", & CLSID );

Obtain the progid Based on clsid.
Winoleapi progidfromclsid (refclsid CLSID, lpolestr * lplpszprogid );
For example, we have defined CLSID_Iapplication, the following code gets the progid
Lpolestr pprogid = 0;
Progidfromclsid (CLSID_Iapplication, & pprogid );
... // You can use pprogid
Cotaskmemfree (pprogid); // do not forget to release

VII. ANSI and Unicode
Unicode is a string of the wide character type, and all Unicode strings are used in COM.

Convert ANSI to Unicode
(1) Use the macro L, for example, clsidfromprogid (L "mapi. folder", & CLSID );
(2) Implement conversion through the multibytetowidechar function, for example:
Char* Szprogid = "mapi. folder ";
Wchar szwideprogid [128];
CLSID;
Long llen = multibytetowidechar (CP_ACP, 0, szprogid, strlen (szprogid), szwideprogid, sizeof (szwideprogid ));
Szwideprogid [llen] = '/0 ';
(3) using the a2w macro, for example:
Uses_Conversion;
Clsidfromprogid (a2w (szprogid), & CLSID );
Convert Unicode to ANSI
(1) Use widechartomultibyte, for example:
// Assume that you already have a unicode string wszsomestring...
CharSzansistring [Max_Path];
Widechartomultibyte (CP_ACP, WC_Compositecheck, wszsomestring,-1, szansistring, sizeof (szansistring), null, null );
(2) Use the w2a macro, for example:
Uses_Conversion;
Ptemp = w2a (wszsomestring );
8. 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

Two 16-bit data (Word) to synthesize 32-bit data (DWORD, lresult, lparam, or wparam)
Long makelong (word wlow, word whigh );
Wparam makewparam (word wlow, word whigh );
Lparam makelparam (word wlow, word whigh );
Lresult makelresult (word wlow, word whigh );

Two 8-bit data (byte) to synthesize 16-bit data (word)
Word makeword (byte blow, byte bhigh );

Obtain the colorref color value from R (red), g (green), and B (blue ).
Colorref RGB (byte byred, byte bygreen, byte byblue );
For example, colorref bkcolor = RGB (0x22,0x98,0X34 );

From colorref color value to RGB color value
Byte Red = getrvalue (bkcolor); // get the red color
Byte Green = getgvalue (bkcolor); // obtain the green color.
Byte Blue = getbvalue (bkcolor); // obtain the blue color

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.