From: http://blog.sina.com.cn/s/blog_4e01df580100dgri.html? Retcode = 0
Int I = 100;
Long L = 2001;
Float F = 300.2;
Double D = 12345.119;
Char username [] = "Who am I ";
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 );
-
- 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.
-
- Cstring variable
STR = "2008 Beijing Olympics ";
Buf = (lpstr) (lpctstr) STR;
- BSTR variable
BSTR bstue =: sysallocstring (L"ProgramMember ");
Char * Buf = _ com_util: convertbstrtostring (bstue );
Sysfreestring (bstue );
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_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 );
-
- General method (for non-com data types)
Use sprintf to complete the 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 ");
- Short INTEGER (INT)
I = atoi (temp );
- Long (long)
L = atol (temp );
- Floating Point (double)
D = atof (temp );
- Cstring variable
Cstring name = 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 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;
- 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
- 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 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;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
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.CodeObtain 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...
Char szansistring [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 );
- (3)
Another option is that C's runtime database provides a simple, convenient, and cross-platform conversion method. To convert a Unicode (such as BSTR) string to an ANSI string, you can call the wcstombs () method (wide character string to multibyte string ):
// Wcstombs (char * ansistring, wchar_t * unicodestring, size_t count );
Char buff [max_length];
BSTR = sysallocstring (L "How did Strings get so uugly? ");
Wcstombs (buff, BSTR, max_length); // P3 = size of target buffer.
Cout <buff <Endl; // pump to console.
Sysfreestring (BSTR );
If you want to convert an ANSI string to Unicode, call the multi byte string to widecharacter string method ):
// Transform an existing char * (ANSI) into a wchar_t * (UNICODE)
Mbstowcs (wchar_t * unicodestring, char * ansistring, size_t count );
Even if we can all accept BSTR (maximizing language independence), we still have an unsolved problem. The string parameters of Win32 APIs are generally ANSI Type. For example, our widely used MessageBox () looks like this:
// This is the MessageBox () method we think we know...
MessageBox (hwnd, lpcstr lptext, lpcstr lpcaption, uint utype );
Based on the method prototype above, it looks like we need to provide two character array constants (lpcstr = long pointer to the constant character array ). However, the reality is always strange. In fact, the root of the Win32 API does not have the MessageBox () method. In fact, this method (all Win32 Methods containing string parameters) is defined as two possible forms:
// Every Win32 function which takes text strings has an ANSI (a) orunicode (W)
// Variation.
# Ifdef Unicode
# Define MessageBox messageboxw
# Else
# Define MessageBox messageboxa
# Endif //! Unicode
In WINNT, when you choose to use Unicode to compile your current project, the Unicode preprocessing flag is defined (in the project | settings menu ). In this case, all the methods in the API are automatically converted to the wide character version. For example, MessageBox () is converted to the following format:
// Under Unicode builds, all strings come through as an array of constantwchar_t.
Messageboxw (hwnd, lpcwstr lptext, maid, uint utype );
In a non-Unicode structure, MessageBox () is converted to an ANSI character version:
// ANSI builds use const char arrays.
Messageboxa (hwnd, lpcstr lptext, maid, uint utype );
We are faced with a dilemma. If we select the Unicode structure, our project can only run correctly under Win NT. If we select a non-Unicode structure, the program can run on all platforms, although on the UNICODE platform (such as Win NT) converts ANSI to Unicode (which means less efficiency ).
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