Conversion between CString, QString, char* (including VC compilation switches)

Source: Internet
Author: User

A const char* (LPCTSTR) pointer passed to unallocated memory.
CString CStr (ASDD);
CONST char* ch = (LPCTSTR) CStr;
Ch points to the same address as the CStr. However, because the use of const guarantees that CH will not be modified, it is safe. 2. Pointer to unallocated memory.
CString CStr = "ASDDSD";
Char *ch = CStr. GetBuffer (CSTR1. GetLength () + 1);
Cstr. ReleaseBuffer ();
Modifying the value of ch points equals modifying the value inside the CStr.
PS: After using CH, do not delete ch, because this will destroy CStr internal space, easy to cause the program crashes.
3. The second usage. Assigns the CString value to the assigned memory char *.
CString cstr1 = "ASDDSD";
int strlength = cstr1. GetLength () + 1;
Char *pvalue = new Char[strlength];
strncpy (PValue, CSTR1, strlength);
4. Third usage. Assigns the CString value to the allocated memory char[] array.
CString cstr2 = "ASDDSD";
int strLength1 = cstr1. GetLength () + 1;
Char charray[100];
memset (charray,0, sizeof (BOOL) * 100); Empties the garbage contents of the array.
strncpy (Charray, CSTR1, strLength1);

If none of the above:
CString conversion to char*
CString origcstring ("Hello, world!");
wchar_t* wcharstring = Origcstring.getbuffer (Origcstring.getlength () +1);
size_t origsize = wcslen (wcharstring) + 1;
size_t convertedchars = 0;
Char *charstring;
Charstring=new char (origsize);
wcstombs_s (&convertedchars, charstring, Origsize, wcharstring, _truncate);
cout << charstring << Endl;
Successful output string "Hello,world"

Reason:
Originally before VC + + 2005, the application by default is to turn off support for Unicode, and in VC2005, the default is to open support for it, CString corresponding string should be Tchar,tchar definition is like this,
#ifdef _UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif
Therefore, you should be able to turn off support for Unicode in your project so that you can convert directly. This is done by right-clicking the character set in the project name-〉property-〉general and selecting NotSetso that the code at the beginning of this article can be executed correctly.


How to convert qstring to char * or vice versa

How can I convert a QString to char* and vice versa? (Trolltech)

Answer:
In order to convert a QString to a char* and then you first need to get a latin1 representation of the string by calling Tola Tin1 () on it which would return a qbytearray. Then call data () on the Qbytearray-get pointer to the data stored in the byte array. See the Documentation:

See the following example for a demonstration:

int main (int argc, char **argv)
{
Qapplication app (argc, argv);
QString str1 = "Test";
Qbytearray ba = Str1.tolatin1 ();
const char *C_STR2 = Ba.data ();
printf ("str2:%s", C_STR2);
return App.exec ();
}
Note that it's necessary to store the ByteArray before-call data () on it, a call like the following
const char *C_STR2 = str2.tolatin1 (). data ();

Would make the application crash as the Qbytearray have not been stored and hence no longer exists.


To convert a char* to a QString your can use the QString constructor that takes a qlatin1string, e.g:

QString string = QString (qlatin1string (C_STR2));

There are several other ways to do this:

Method One-----------------------------------------
#define G2U (s) (Qtextcodec::codecforname ("GBK")->tounicode (s))
#define U2G (s) (Qtextcodec::codecforname ("GBK")->fromunicode (s))

QString str;
Qcstring CStr;

str = g2u ("Chinese input");
CStr = u2g (str);

Qcstring has such an overloaded operator
operator Const char * () const

Can do this
printf ("%s\n", (const char*) CStr);
or copy it out.
Char buf[1024];
strcpy (buf, (const char*) CStr);

Method Two-----------------------------------------
If the Chinese system

Direct use (const char*) str.local8bit ()
For example
printf ("%s", (const char*) str.local8bit ());

STR is a qstring

Method Three-----------------------------------------
Char str[64];
Qtextcodec *textcod = Qtextcodec::codecforname ("GBK");
qcstring string1 = Textcod->fromunicode (Listbox1->currenttext ());
strcpy (STR,STRING1);

Qstring and std::string

From char* to Qstring can be converted from fromlocal8bit ()
Std::string has C_str () function to convert to char*

Qstring have Toascii () you can look at it. Again my carelessness made a big mistake, I re-check the QT document, the original QT can be directly from the std::wstring to produce a qstring, with qstring::fromstdwstring ( Const std::wstring &) This static member function can be. I tried to try Std::string's C_STR () returned char * constructed qstring can no longer save the original Chinese information, and the qstring constructed with std::wstring may use Qdebug () to output the original Chinese information

GB encoding and UTF8 encoding conversion
Add this sentence after the main function app:

Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("GB18030"));

Then there is the string conversion method from UTF8 encoding to GB encoding:

QString UTF8_TO_GB (QString strText)
{
Return Qstring::fromutf8 (Strtext.tolocal8bit (). data ());
}

As for from GB to UTF8, that is often used:

QString Gb_to_utf8 (char *strtext)
{
Return Qstring::fromlocal8bit (StrText);
}

Reference: http://www.cppblog.com/Alina-zl/archive/2008/11/19/67323.html

Conversion between CString, QString, char* (including VC compilation switches)

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.