In the case of common ASCII encoding, the system stores cstring in a way similar to char * by default (in my opinion ).
For example, you can declare and assign a cstring as follows:
Char * charstr = "kenko ";
Cstring CSTR = charstr;
In ASCII encoding, cstring reads the memory location of the pointer behind it as the input stream characters one by one into cstring.
However, in the _ Unicode macro definition, all are changed to the width byte by default. Then the cstring storage method will be in the form of wide bytes.
Therefore, to change cstring to wchar_t * as long as the following:
Cstring to wchar *:
Wchar_t unicodestr [255];
Wcscpy (unicodestr, CSTR );
To assign a value to cstring, you must assign a value in the form of a wide byte (of course, directly assign a value to cstring using char *, and the system will automatically convert the value ).
For example, if a webpage is intercepted and the input byte stream is still ASCII, a problem may occur.
During the programming process, I assign values to the ASCII encoded byte stream, which leads to the failure to find strings in the future. After finding the root cause of the problem, the wchar_t * obtained from cstring is forcibly converted to char *. The root cause of the problem lies inCodeThe comment is written.
The Code is as follows: it refers to the use of cinternetsession to intercept webpage content.
1 // Establish connections, send requests, and receive data
2 Cinternetsession httpsession;
3 Cstring line;
4 Cstring result;
5
6 Cinternetfile * Webfile = NULL;
7
8 If (URL = Null) Return False ;
9 Try
10 {
11 Wchar_t unicodestr [ 255 ];
12 Wcscpy (unicodestr, ccombstr (URL ));
13 Webfile = (Cinternetfile * ) Httpsession. Openurl (unicodestr );
14 If (Webfile)
15 {
16 Int I = 0 ;
17 While (Webfile -> Readstring (line) && I < 300 )
18 {
19 Result = Result + Line;
20 I ++ ;
21 }
22 }
23 Delete webfile;
24 Httpsession. Close ();
25 Wchar_t unicodestr2 [ 3000 ];
26 Wcscpy (unicodestr2, result );
27 /* Because cstring inputs an ASCII byte stream when reading a webpage,
28 However, the system accepts the width byte by default. Therefore, the gb2312 ASCII encoded byte stream of the webpage is read every two bytes.
29 For example, if the webpage already contains 1000 characters, the cstring getlength obtained by the above Code is only 500
30 So after converting it to wchar_t *, do a forced conversion. */
31 Char * Charresult = ( Char * ) Unicodestr2;
32 String Strresult (charresult ); // Char * to string
33
34 // Obtain the specific information from the HTML file.
35 Int Begin = Strresult. Find ( " <Body> " ) + 6 ;
36 Int End = Strresult. Find ( " </Body> " );
37 Strresult = Strresult. substr (begin, end - Begin );
38 Strcpy (resultstring, strresult. c_str ());
39
40 Return True ;