About the usage and conversion of strings

xiaoxiao2021-03-06  91

1: Use CString to include file AFX.H, such as the USE MFC IN A Static Library, then add #include to add #include to use CString. 2: Wchar ch = L 'in'; in CHAR CH = ''; the difference is the first use of Unicode encoding, the second method is generally not often used, such as Wchar STRA [2] = {L ' ', 0}; // Open the Options menu of the VC, select the value of STRA after selecting the Display Unicode Strings in the Debug tab.

Wchar strb [2] = {'Chinese', 0};

CSTRING STRC;

STRC = ((char *) strb) [1];

STRC = ((char *) strb) [0]; // STRC == "in"

3: cstring's allocsystring () member function; convenient to convert a string into a Unicode form. Remember to call :: sysfreestring () function release string after using the Unicode string.

4: cstring :: allocsystring () or :: sysallocstring getting the string is not a normal Unicode string, and the four bytes before it stores the length of the queue:

CString strd = "asdf";

BSTR strd = strc.allocsystring ();

Long i = * ((long *) strd - 1); // i == 8; one Unicde character is 2 bytes, so the length of STRD is 8 bytes.

4: UTF-8 code is converted to a normal string:

#include "windows.h"

Int main (void)

{

CHAR STR [256] = {(Char) 0xE4, (char) 0xBD, (char) 0xA0, (char) 0xE5,

(char) 0xA5, (char) 0xBD, (char) 0x61, (char) 0x62, (char) 0x63, (char) 0}; // segment UTF-8 encoding

Wchar * straad;

INT i = MULTIBYTOWIDECHAR (CP_UTF8, 0, (Char *) STR, -1, NULL, 0);

Stra = new wchar [i];

MultibyToWideChar (CP_UTF8, 0, (CHAR *) STR, -1, STRA, I);

I = WideChartomultibyte (CP_ACP, 0, Stra, -1, NULL, 0, NULL, NULL);

Char * strb = new char [i];

Widechartomultibyte (CP_ACP, 0, STRA, -1, STRB, I, NULL, NULL);

// STRB is what

delete [] straw;

delete [] strb;

Return 0;

}

5: In the conversion_bstr_t is the most flexible, he provides direct conversion of Unicode to the general string:

#include

_BSTR_T straw;

Char * strb = "Chinese";

STRA = strb; wchar * strc = straw;

Long i = * ((long *) STRC - 1); // i is also the byte length of the string

Char * strd = stra;

Return 0;

Author Blog:

http://blog.9cbs.net/bluebohe/

related articles

VC Commissioning Getting Author: class (B) Arong a manual to read and write INI files Visual C programming skills shortcut VC: VC common problems entry summary (c)

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

New Post(0)