CString string char * character conversion between char (multiple methods), cstringchar

Source: Internet
Author: User

CString string char * character conversion between char (multiple methods), cstringchar

When writing a program, we often encounter various types of conversions, such as conversion between char * CString strings. First, explain the meaning of the three.

CString is a useful data type. They greatly simplify many operations in MFC (applicable to the MFC framework), making it much easier for MFC to perform string operations. Header file # include <afx. h>

C ++ is a string with powerful functions. To use the string class in Standard C ++, you must include # include <string> // note that it is <string>, not <string. h>,. h is the header file in C language. Char * is used to specify a string ending with '\ 0.

Perform conversion using the following methods:

// CharConvert. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> # include <afx. h ># include <string> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {// This method applies to "Multi-Byte". Otherwise, 'strcpy' appears ': cannot convert parameter 2 from 'unsigned short * 'to 'const char *'/* CString str ("Hello zzu "); cout <"the CString is" <str <endl; char a [100]; strcpy (a, str ); cout <"the convert to char * is" <a <"(char *)" <endl; * // convert CString to string // CString c _ Str1 ("Hello Zhengzhou University"); // string str; // str = c_str1.GetBuffer (0); // c_str1.ReleaseBuffer (); // otherwise, the space occupied by the buffer zone is not released. // cout <str <endl; // cout <"\ n" <c_str1 <endl; // string to CString/* string str1 ("Hello College of Information Engineering"); CString c_str2; c_str2 = str1.c _ str (); // c_str (): generate a const char * pointer pointing to the array terminated with null characters. Cout <"the CString is" <c_str2 <endl; cout <"the string is" <str1 <endl; * // string converted to const char * // Method 1: // string str2 ("Hello College of Information Engineering"); // const char * str3; // constant pointer // str3 = str2.c _ str (); // cout <"string is" <str2 <endl; // cout <"const char is" <str3 <endl; // Method 2: // * string str ("Hello College of Information Engineering "); // const char * c_str; // c_str = str. data (); // cout <"string Is "<str <endl; // cout <" const char is "<c_str <endl; * // string is directly converted to char * // * string s1 = "abcdefg"; // char * data; // int len = s1.length (); // data = (char *) malloc (len) * sizeof (char); // s1.copy (data, len, 0 ); // cout <len <endl; // cout <data <endl; * // string. usage of copy () // size_t length; // char buffer [8]; // string str ("Test string ...... "); // length = str. copy (buffer,); // from buffer6, number 7 in the future, equivalent to [buffer [6 ], Buffer [6 + 7]) // buffer [length] = '\ 0'; // Add' \ 0' so that the buffer ends with buffer [length; // cout <"buffer contains:" <buffer <endl; // char * is converted to string // char * to string/* char * c_str = "zzu "; string str (c_str); cout <"the c_str" <c_str <endl; cout <"the string is" <str <"and length is" <str. length () <endl; * // char a [] = "asd"; // cout <strlen (a) <endl; // Why is 3 displayed, not \ 0? // char * To CString // char * c_str1 = "zzu"; // CString str; // can be directly converted because CString is overloaded (applicable in Multi-byte character sets) /// str. format ("% s", c_str1); // cout <"the c_str1 is" <c_str1 <endl; /// cout <"the CString is" <str <endl; // Method 1: Use API: WideCharToMultiByte for conversion (used, valid) // CString str = CString ("This is an example! "); // Int n = str. getLength (); // String Length. // int len = WideCharToMultiByte (CP_ACP, 0, str, n, NULL, 0, NULL ); // calculate the str length by Byte // char * pChStr = new char [len + 1]; // in bytes // WideCharToMultiByte (CP_ACP, 0, str, n, pChStr, len, NULL, NULL); // converts wide bytes to multi-byte encoding // pChStr [len] = '\ 0 '; // do not ignore the end mark. // when the end is used up, remember to delete [] pChStr to prevent memory leakage. return 0 ;}

At the same time, it should be noted that when we write a program in flat mode, we 'd better find out the encoding methods in our compilation environment. Different encoding methods can cause some character conversion failures. When we start programming, we need to consider which encoding method should be used in order to avoid changing the encoding method, but the code has many errors (which is a headache ), for example, Chinese characters in the sqlite database are garbled because the encoding method is inconsistent with the default encoding method in the database. This must be kept in mind when writing a program.


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.