CSTRING

xiaoxiao2021-03-06  39

CString is unfamiliar with MFC Programmer. It is simpler than std :: basic_string, and it automatically supports Unicode, unlike STL or Standard C , you need to use WString / WStringStream / Wchar (unicode) or string / stringstream / char (ASCII / MBCS) to distinguish and special processing The problem is that the MSDN does not provide its use of it. Many people still use some complicated functions to make simple things. I will give some suggestions on part 1 ------- - word string - digital converted string - strings into numbers - Several char * strings are converted to CSTRING - CSTRING strings LPCTSTR string - CSTRING strings are converted to a char * string using getBuffer - CSTRING string to LPTSTR. Part 2 -------- CSTRING string to convert to a BSTR - BSTR string to convert to cstring - Variant strings turn CSTRING - CSTRING Strings Go to StringTable Resources - CSTRING Strings smoothly manages - CSTRING Errors in Programmer - word string combined with Char country [] = "malaysia"; char state [] = "kl"; char * Address = Malloc (Strlen (Country) 1); strcpy (address, country); strcat (address, state); simpler method CString Country; CString State ("kl") CSTRING Address = Country State; - Digital converted strings General people will use char pszbuffer [20]; int I = 3445; _TOA (I, Pszbuffer, 10); Strcat (strDest, pszbuffer) is simpler method CString S; S.Format ("The Total IS% D"), TOTAL); - String to convert to digital ordinary people will use int itemp = atoi (PSZBuffer); better method cstring decimal = _t ("4011"); int i = _ttoi (decimal)); because _ttoi is a unicode aware - cstring string to convert to LPCTSTR string CString s ("Anthony Yio"); LPCTSTR P = S; or LPCTSTR P = S.Operator LPCTSTSTR (); - cstring String to Char * word String use GetBuffer CString Str (...); LPTSTR PTEMP = Str.getBuffer (1024); str.releasebuffer (); Note: Do not change the string of PTEMP, when you have not called S.ReleaseBuffer (); function after. Because it will be changed to STR. - CSTRING string goes lptstr. Cstring starts = (lpctstr) strtobeconverted; Note: PSZTEMP cannot be changed. Do not do another variable unless you use StrcPy.

Part 2 ------- - CString strings into bstr cstring s; s = _t ("hello world"); bstr b = s.allocsystring () :: sysfreestring (b); Remember Call functions :: SysfreeString (b), otherwise there will be Memory Leak - BSTR strings to convert to cstring full function code. CString Convert (BSTR B) {CSTRING S; if (b == null) Return S; // Empty for Null BSTR #IFDEF Unicode S = B; #ELSE LPSTR P = S.GetBuffer (Systringlen (B) 1); :: widechartomultibyte (cp_acp, // Ansi code page 0, // no flags b, // source widechar string -1, // assume nul-terminated p, // target buffer systringlen (b) 1, // target buffer Length null, // use system default char NULL); // Don't Care if Default Used s.releasebuffer (); #ENDIF RETURN S;} ANSI sector is complicated than Unicode strings. Because the BSTR itself is a Unicode string. CString has a constructor to accept BSTR, but pay attention to NULL because CString is not sensitive to BSTR NULL management. So the code should be written.

IF (b == null) return s; // Empty for null bstr ANSI is also MBCS. So you want to use: widechartomultibyte Transfer BSTR in the Variant (unicode) string to MBCS. - Variant strings to cstring variant vadata; Vadata = m_com.YourMethodhere (); assert (vadata.vt == vt_bstr); cstring strdata (vadata.bstrval); or more complete function cstring varianttoTring (variant * va) {cstring s; switch (va-> vt) { / * vt * / case vt_bstr: return cstring (vadata-> bstrign); case vt_bstr | vt_byref: returnif; (* vadata-> pbstrign); case vt_i4: s.format (_t ("% d"), VA-> Lval); Return S; Case VT_I4 | VT_BYREF: S.Format (_T ("% D"), * VA-> PLVAL); Case VT_R8: S.Format (_T ("% f"), VA-> DBLVAL) RETURN S; ... Remaining Cases Left as an adercise for the reader default: assert (false); // unknown variant type (this assert is optional) Return CString ("");} / * vt * /} - cstring string to STRINGTABLE resources CString s; s.LoadString (IDS_WHATEVER); or WIN32 style CString s; s.LoadString (IDS_WHATEVER); CString t (MAKEINTRESOURCE (IDS_WHATEVER)); - CString smooth management code string 1 CString s = _T ( "Hello") _T ("I am ") " Saving Memory Space "; will be compared to code 2 cstring s = _t (" hello "); s = _t (" i "); s = _t (" am "); s = _t (" Little "); S = _T (" inefficient "); there are several primary buffers for the CString variable. If it is code 2, the primary buffer of the cstring variable will be relatively large. Because the Compiler compiler wants to keep the CString variable to the maximum The possibility. The Compiler compiler budget is not bigger. - CSTRING Error for Programmer conflict CString str = _t ("Hello); if (str ==" Hello ") ... this will crash. Should be cstring str = _t (" hello); if (str .comparenocase) Hello ") == 0) ... Try to use the function in cstring to make a string management. Don't use '=' Operator. Unless you know what you are doing. Because CString does not Type Safe. It can accept any Data Type, but this is very dangerous.

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

New Post(0)