VC ++ type conversion finishing (reposted)

xiaoxiao2021-03-05  53

Servers: MaxView (Sworm) Mode: Credit: 100 Forum: VC / MFC Basics Questions: 0 Reposted: 2 Published: 2003-07-15 17: 07: 03z

According to my past habits, I still say a few words before I started the topic:

1. Personally think that learning programming is difficult! You have three, five thousand functions, and then start programming, and it is sure to come to the hand;

2, when I reply to a friend's post, some people say that my statement has a problem, I don't know if there is any problem, so this article will make everyone as a speed check manual, and on the other hand, I hope that everyone may point out there. The problem;

Start!

1, CString to char * After a type forced conversion, the CString type can be converted to char *, for example: cstring cstr = "Hello, World!"; Char * zstr = (char *) (lpctstr) CSTR;

2, char * to cstringchar * type can be directly converted to CString, complete automatic conversion, for example: char * zstr = "Hello, World!"; Cstring cstr = zstr;

3, CSTRING to LPCSTR converts CString into LPCSTR, requiring CString length, for example: cstring cstr = _t ("Hello, World!"); Int Nlen = cstr.getLength (); lpcstr lpszbuf = cstr.getBuffer (Nlen) ;

4, CString to LPSTR This and the third tip is the same, for example: cstring cstr = _t ("Hello, World!"); Int Nlen = str.getlength (); lpstr lpszbuf = str.getBuffer (Nlen);

5, char [] to int convert the string type into an integer type, you can use the ATOI function, for example: char C [10]; int N; n = atoi (c);

6, char [] to float and the 5th tip, using the atof () function can be converted to Float type, for example: char C [10]; float f; f = ATOF (C);

7. Char * to int and the 5th tips are exactly the same, for example: char * str = "100"; int I; i = ATOI (STR);

Everyone is coming to follow the post, put all types of conversion, benefiting the children ... Reply: JX02 (WANGW) () Reputation: 99 2003-07-15 17: 50: 28Z score: 0 See the structure of the object, conversion Member function, all conversion is Top Reply to: kxcc_sx (Happy Pen) () Reputation: 101 2003-07-15 17: 54: 19Z Score: 0 VC Common Data Type Use Conversion Detail 2002-7-29 12: 40:48 PCVC.NET Cheng Peijun Readings: 11840 Friends who have just contacted VCs often confuse the conversion of many data types, this article will introduce some common data types.

Let's first define some common type variables to explain

INT i = 100; long L = 2001; float f = 300.2; double d = 12345.119; char username [] = "Female Jeruchan"; Char Temp [200]; char * buf; cstring str; _variant_t v1; _bstr_t v2; First, other data types are converted to strings

Short integer (INT) ITOA (I, TEMP, 10); /// Transforms i into a string into TEMP, the last digit represents decimal ITOA (I, TEMP, 2); // Press binary to convert Long integer (LONG) LTOA (L, TEMP, 10);

Second, get a pointer to the string from other variables containing strings

CString variable str = "2008 Beijing Olympics"; BUF = (LPSTR) (LPCTSTR) Str; BSTR type _variant_t variable v1 = (_BSTR_T) "programmer"; buf = _com_util :: convertBSTRTSTRING ((_ bstr_t) v1);

Third, the string is converted to other data type STRCPY (TEMP, "123");

Short integer (int) i = ATOI (TEMP); long integer (long) L = atol (TEMP); Double) D = ATOF (TEMP)

Fourth, other data types are converted to CString using the member function format using CString, for example:

Integer (int) str.format ("% d", i); floating point number (FLOAT) Str.Format ("% f", i); string pointer (char *), etc. has been supported by the CString constructor Can be assigned directly STR = UserName;

Five, BSTR, _BSTR_T and CCOMBSTR

CCOMBSTR, _BSTR_T is package to BSTR, BSTR is a 32-bit pointer to a string. Char * Convert to BSTR can be like this: BSTR B = _com_util :: ConvertStringTobstr ("Data"); /// Use Need to add header files Comutil.h, you can use char * p = _com_util :: ConvertBSTRTOSTRING (B);

Sixth, Variant, _variant_t and Colevariant

Variant's structure can refer to the definition of struct tagvariant in the header file VC98 / include / OAIDL.H. For the assignment of the variant variable: first assign a value to the VT member, specify the data type, and then assign a value of the same data type in the federation, take an example: Variant Va; int a = 2001; va.vt = vt_i4; // indicate Integer data va.lval = a; // / assignment

For Variant that doesn't assign values, it is best to use Void Variantinit (Variantarg Far * Pvarg); in initialization, the essence is set to vt_empty, the following table, the following table, the following table, the following table, the corresponding relationship of 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 scode; VT_ERROR CY cyVal; VT_CY DATE 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 * FAR * ppunkVal; VT_BYREF | VT_UNKNOWN IDispatch FAR * FAR * ppdispVal; VT_BYREF | VT_DISPATCH SAFEARRAY FAR * FAR * pparray; VT_ARRAY | * VARIANT FAR * pvarVal; VT_BYREF | VT_VARIANT void FAR * byref; VT_BYREF_variant_t is VARIANT wrapper classes which are assigned to using casts Its constructor automatically handles 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 _variant_t. Please refer to the following example: Colevariant V3 = "String", V4 = (long) 1999; CString Str = (BSTR) v3.pbstrval; long; long;

Seven, other

In the processing of messages, we often need to decompose 32-bit data (DWORD) such as WPARAM or LPARAM to two 16-bit data (Word), for example: lparam lparam; Word Lovalue = Loword (LPARAM); /// Take 16 Word HiValue = HiWord (LPARAM); /// Take a 16-bit data for 16-bit data (Word) we can use the same method to decompose to two 8-bit data (byte), for example: Word Wvalue; Byte Lovalue = Lobyte (wvalue); /// Take a low 8 bYTE HIVALUE = Hibyte (wvalue); /// Take 8 posts: This article is in a hurry, the wrong is inevitable, welcome to the letter. Int-> Str Itoa, Atoidouble- Str fToA, ATOF

_BSTR_T, _VARIANT_T, CSTRING, LONG, etc. Let's see the detailed example of me, look at the following first _variant_t and _bstr_t these constructor and operator = have heavy loads, other type to _variant_t assignment: _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 (); _ varia nt_t (const DECIMAL & decSrc) throw (); _ variant_t (BYTE bSrc) throw (); operator = overloaded form: _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 = (constly & 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, take an example: double f = 1.0_variant_t v; v = f; // is a legal look at the overload form of Operator = Know the CString str = "DDD" _variant_t V; v = str.allocsystring () or v = (_ bstr_t) (char *) STR;

_variant_t Convert to other forms You first must determine what form of Double F you want to transform; _variant_t vf = v.dblval can be or f = (double) V;

Attached: _variant_t Operator short () const thrt (_COM_ERROR); operator long () const throw (_COM_ERROR); operator float () const throw (_Com_ERROR); operator double () const throw (); 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);

转载请注明原文地址:https://www.9cbs.com/read-32010.html

New Post(0)