Int I = 100;
Long L = 2001;
Float F = 300.2;
Double D = 12345.119;
Char username [] =" Cheng Pei Jun ";
Char temp [200];
Char * Buf;
Cstring STR;
_ Variant_t V1;
_ Bstr_t V2;
1. convert other data types to strings
1.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
2.Long Integer(Long)
Ltoa (L, temp, 10 );
3.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.
4. cstringVariable
STR = "2008 Beijing Olympics ";
Buf = (lpstr) (lpctstr) STR;
5. BSTRVariable
BSTR bstrvalue =: sysallocstring (L "programmer ");
Char * Buf = _ com_util: convertbstrtostring (bstrvalue );
Sysfreestring (bstrvalue );
Afxmessagebox (BUF );
Delete (BUF );
6. ccombstrVariable
Ccombstr bstrvar ("test ");
Char * Buf = _ com_util: convertbstrtostring (bstrvar. m_str );
Afxmessagebox (BUF );
Delete (BUF );
7. _ bstr_tVariable
_ Bstr_t is the encapsulation of BSTR. It is easy to use because = operator has been overloaded.
_ Bstr_t bstrvar ("test ");
Const char * Buf = bstrvar; // do not modify the Buf content
Afxmessagebox (BUF );
8.General Method(For non-ComData Type)
UseSprintfComplete Conversion
Char buffer [200];
Char c = '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 ");
1.Short integer(INT)
I = atoi (temp );
2.Long Integer(Long)
L = atol (temp );
3.Floating Point(Double)
D = atof (temp );
4. cstringVariable
Cstring name = temp;
5. BSTRVariable
BSTR bstrvalue =: sysallocstring (L "programmer ");
/// Complete the use of bstrvalue
... Sysfreestring (bstrvalue );
6. ccombstrVariable
Ccombstr variables can be directly assigned values.
Ccombstr bstrvar1 ("test ");
Ccombstr bstrvar2 (temp );
7. _ bstr_tVariable
_ Bstr_t type variables can be directly assigned values
_ Bstr_t bstrvar1 ("test ");
_ Bstr_t bstrvar2 (temp );
Iii. convert other data types to cstring
Use the cstring member function Format for conversion. For example:
1.INTEGER (INT)
Str. Format ("% d", I );
2.Float)
Str. Format ("% F", I );
3.Data Types supported by cstring constructors, such as string pointers (char *), can be directly assigned values.
STR = username;
4.For data types not supported by format, you can convert the data type to char * by using the method described above, and then assign the value to the cstring variable.
Iv. BSTR, _ bstr_t and ccombstr
1. ccombstr is the encapsulation of BSTR by ATL, _ bstr_t is the encapsulation of BSTR by C ++, and BSTR is a 32-bit pointer, but it does not directly point to the buffer zone of the string.
Char * can be converted to BSTR 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 overload 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
1. 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:
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.
Char cval; // vt_i1.
Unsigned short uival; // vt_ui2.
Unsigned long ulval; // vt_ui4.
Int intval; // vt_int.
Unsigned int uintval; // vt_uint.
Char far * 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.
2._ 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;
2.1The 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;
6. Other COM Data Types
1.Obtain CLSID Based on progid
Hresult clsidfromprogid (lpcolestr lpszprogid, lpclsid pclsid );
CLSID;
Clsidfromprogid (L "mapi. folder", & CLSID );
2.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.
1.Convert ANSI to Unicode
(1)Implemented through the macro l,For example, clsidfromprogid (L "mapi. folder", & CLSID );
(2)Implement conversion using the multibytetowidechar FunctionFor 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)Implemented through the a2w macro,For example:
Uses_conversion;
Clsidfromprogid (a2w (szprogid), & CLSID );
2.Convert Unicode to ANSI
(1)Use widechartomultibyteFor example:
// Assume that you already have a unicode string wszsomestring...
Char szansistring [max_path];
Widechartomultibyte (cp_acp, wc_compositecheck, wszsomestring,-1, szansistring, sizeof (szansistring), null, null );
(2)W 2a Macro implementationFor example:
Uses_conversion;
Ptemp = W 2a (Wszsomestring );
8. Others
1.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.
2.For 16-bit data (Word), we can use the same method to split it into two 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
3.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 );
4.Two 8-bit data (byte) to synthesize 16-bit data (word)
Word makeword (byte blow, byte bhigh );
5.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 );
6.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
IX. Precautions
To use convertbstrtostring, add the header file comutil. H, add comsupp. lib to setting, or directly add # pragma comment (Lib, "comsupp. lib ")
Postscript: This article is written in a hurry. errors are inevitable. please correct me.
Question about converting BSTR data to cstring data?
When I convert BSTR type data to cstring or "char * type" data, it is found that the BSTR type string is short, and when it is long, it will appear
Memory read/write error. (This is true for all tests in NT and 2000 .)
According to what you said:
1) string pointers (char *) and other data types supported by cstring constructors can be directly assigned STR = username;
2) When B is of the BSTR type, you can use
Char * P = _ com_util: convertbstrtostring (B );
So the following is correct:
Cstring CSTR;
BSTR;
....
CSTR = com_util: convertbstrtostring (BSTR );
...
However, when BSTR is very large (in fact, it will be large), there will be a memory read/write error, somehow.
In addition, I found that CSTR = com_util: convertbstrtostring (BSTR );
It can be simplified to CSTR = BSTR, but this problem also occurs when BSTR is large.
Please help me! Urgent. Thank you!
How to convert (list *) fileip. Bian-> Liang
Author: jakiesun Author: Converting cstring into lpctstr 20:08:48
I wrote such a piece of code before.
Void function ()
{
Cstring STR, str1, str2;
Function (char *) (lpctstr) str1 );
STR = str1;
... // The debugging result shows that the str2 value changes with the change of str. Could you explain why? If you have any answers, please notify me
Wangshaohong@sohu.com, TX first
}
Added lib support Author: sharpening the knife Huo published on: 11:32:12
If this parameter is not added, an error occurs. Add comsupp. lib to setting or directly # pragma comment (Lib, "comsupp. lib ")
Microsoft believes that the same error will occur if the default call Convention is set to _ cdecl.