VC string Conversion Common functions

Source: Internet
Author: User

Recently doing some of the VC's ActiveX widgets, often encounter string processing problems, Mad Csdn and MSDN, the results are not ideal. First of all, the relevant processing function is passed in the vc++6.00 test. Perhaps a lot of people do not understand, now what age, but also with VC6.0, in fact, VC development plug-in has a benefit is the deployment of a convenient time, regardless of whether the browser-side user installed the. NET Framework. The. NET Framework is getting bigger with the launch of a new version, so that users spend so much time installing that behemoth that they actually use very little, so many people will not do it. In addition, the server is developed by Java, and it is more impossible to add. Net. Therefore, VC development of things will not worry about these things. Say more ...

VC programming often encounter the problem of string conversion, and now do a summary to facilitate later use.

(1) char* converted into CString
If you convert char* to CString, you can use Cstring::format in addition to directly assigning values. For example:
Char charray[] = "char test";
TCHAR * p = _t ("Char test");( or LPTStr p = _t ("Char test");)
CString thestring = Charray;
Thestring.format (_t ("%s"), Charray);
TheString = p;

(2) CString converted into char*

If you convert the CString class to a char* (LPSTR) type, you often use the following three methods:

method One, using cast。 For example:

CString TheString ((_t ("Char test"));
LPTSTR lpsz = (LPTSTR) (LPCTSTR) thestring;

method Two, using strcpy。 For example:

CString TheString ((_t ("Char test"));
LPTSTR lpsz = new Tchar[thestring.getlength () +1];
_tcscpy (Lpsz, thestring);

It should be stated that the second parameter of the strcpy (or _tcscpy) is a const wchar_t* (Unicode) or a const char*
(ANSI), the system compiler will automatically convert it.

method Three, using Cstring::getbuffer
If you need to modify the content in the CString, it has a special method that can be used, and that is GetBuffer, which is the function of returning a writable buffer pointer.
If you're just going to modify a character or truncate a string, for example:
CString s (_t ("Char test"));
LPTSTR p = s.getbuffer ();
LPTSTR dot = strchr (P, '. ');
Add the code that uses p here
if (P! = NULL)
*p = _t (' + ');
S.releasebuffer (); Release immediately after use so that other CString member functions can be used
In this range between GetBuffer and ReleaseBuffer, you must not use any method of this buffered CString object that you want to manipulate. Because
The integrity of the CString object is not guaranteed until ReleaseBuffer is called.

Other general Conversions

Define some common type variables to illustrate
int i = 100;
Long L = 2001;
float f=300.2;
Double d=12345.119;
Char username[]= "XX";
Char temp[200];
Char *buf;
CString str;

One, other data types converted to strings
short integer (int)
Itoa (i,temp,10); /converts I to a string in temp, and the last number indicates decimal
Itoa (i,temp,2); Converting in binary mode
Long integer type
Ltoa (l,temp,10);

second, get a pointer to the string from another variable that contains a 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);

Third, string conversion to other data types
strcpy (temp, "123");
short integer (int)
i = atoi (temp);
Long integer type
L = ATOL (temp);
Floating point (double)
D = atof (temp);

Iv. Conversion of other data types to CString
Use the CString member function format to convert, for example:
Integer (int)
Str. Format ("%d", I);
Floating point (float)
Str. Format ("%f", I);

string pointers (char *), and so on, can be directly assigned to data types that are supported by the CString constructor
str = username;

Five, BSTR, _bstr_t and CComBSTR
CComBSTR, _bstr_t is the encapsulation of a BSTR, a BSTR is a 32-bit pointer to a string.
char * Conversion to a BSTR can be like this: BSTR
B=_COM_UTIL::CONVERTSTRINGTOBSTR ("Data");
Need to add header file before use Comutil.h
Conversely, Char *p=_com_util::convertbstrtostring (b) can be used;

six, the variant into CString

Varianttostring (_variant_t varvalue)
{
if (VARVALUE.VT!=VT_BSTR)
{
Varvalue.changetype (VT_BSTR);
}
Return (char*) _bstr_t (varvalue.bstrval);
}

Note: Add the header file #include "comdef.h"

convert Variant to Long method

Varianttolong (ConstVARIANT &var){ LongR;Switch(var. VT) {  CaseVT_UI2://USHORTR =var. Uival; Break;  CaseVT_UI4://ULONGR =var. Ulval; Break;  CaseVt_int://INTR =var. intval; Break;  CaseVt_uint://UINTR =var. Uintval; Break;  CaseVT_I4://LONGR =var. lval; Break;  CaseVT_UI1://BYTER =var. BVal; Break;  CaseVT_I2:// ShortR =var. ival; Break;  CaseVT_R4://FLOATR = (Long)var. Fltval; Break;  CaseVT_R8://DOUBLER = (Long)var. dblval; Break; default: R= -1;//the value cannot be converted  Break; } returnR;}

VC string Conversion Common functions

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.