Conversion of qstring cstring char

Source: Internet
Author: User

From: http://hi.baidu.com/koko200147/blog/item/7e3cad828c9b9bb66d8119cb.html

Thanks to the original author!

Qstring is Unicode. UTF16 in qt4.

Qstring fromascii (const char * STR, int size =-1)
Qstring fromlatin1 (const char * STR, int size =-1)
Qstring fromlocal8bit (const char * STR, int size =-1)
Qstring fromrawdata (const qchar * Unicode, int size)
Qstring fromstdstring (const STD: string & Str)
Qstring fromstdwstring (const STD: wstring & Str)
Qstring fromucs4 (const uint * Unicode, int size =-1)
Qstring fromutf8 (const char * STR, int size =-1)
Qstring fromutf16 (const ushort * Unicode, int size =-1)
Qstring fromwchararray (const wchar_t * string, int size =-1)

Qstring-> STD: String
Qstring: tostdstring (), qstring: tostdwstring ()

BSTR <-> qstring
BSTR bstr_str;
Qstring q_str (qchar *) bstr_str, wcslen (bstr_str ))
Bstr_str = sysallocstring (q_str.utf16 ())

Remember use sysfreestring on BSTR

Qstring <-> lpcstr
Qstring: tolocal8bit (). constdata ()
Qstring fromlocal8bit (const char * STR, int size =-1)

Qstring <-> lpcwstr
Qstring: UTF16 ()
Qstring fromutf16 (const ushort * Unicode, int size =-1)

Qstring <-> cstring
Cstring c_str (qstring: UTF16 ())
Qstring fromutf16 (lpctstr (c_str ))

Pass the const char * (lpctstr) pointer to the unallocated memory.
Cstring CSTR (asdd );
Const char * Ch = (lpctstr) CSTR;
Ch points to the same address as CSTR. However, since the const is used to ensure that CH will not be modified, the data is safe. 2. It is passed to the pointer of unallocated memory.
Cstring CSTR = "asddsd ";
Char * Ch = CSTR. getbuffer (cstr1.getlength () + 1 );
CSTR. releasebuffer ();
// Modify the value that CH points to is equal to the value in CSTR.
// PS: After the CH is used up, the delete ch is not used, because this will destroy the internal space of CSTR and cause program crash.
3. Second usage. Assign the cstring value to the char * of the allocated memory *.
Cstring cstr1 = "asddsd ";
Int strlength = cstr1.getlength () + 1;
Char * pvalue = new char [strlength];
Strncpy (pvalue, cstr1, strlength );
4. Method 3: Assign 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); // clears the junk content of the array.
Strncpy (charray, cstr1, strlength1 );

If none of the above works:
Cstring 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;
The string "Hello, world" is successfully output"

Cause:
Previously, before VC ++ 2005, Unicode support was disabled for applications by default. In vc2005, support for Unicode was enabled by default. The string corresponding to cstring should be tchar, tchar is defined as follows,
# Ifdef _ Unicode
Typedef wchar_t tchar;
# Else
Typedef char tchar;
# Endif
Therefore, Unicode support should be disabled in the project so that direct conversion can be performed. In this case, right-click the project name-> property-> character set in general and select notset. In this way, the code at the beginning of this article can be correctly executed.

Qstring gb_to_utf8 (char * strtext)
{
Return qstring: fromlocal8bit (strtext );
}

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 *, then you first need to get a Latin1 representation of the string by calling tolatin1 () on it which will return a qbytearray. then call data () on the qbytearray to get a 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.exe C ();
}
Note that it is necessary to store the bytearray before you call data () on it, a call like the following
Const char * c_str2 = str2.tolatin1 (). Data ();

Will make the application crash as the qbytearray has not been stored and hence no longer exists.

To convert a char * To A qstring you can use the qstring constructor that takes a qlatin1string, e. g:

Qstring string = qstring (qlatin1string (c_str2 ));

 

There are other methods:

Method 1 -----------------------------------------
# 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 overload Operator
Operator const char * () const

Yes.
Printf ("% s/n", (const char *) CSTR );
Or copy it out.
Char Buf [1024];
Strcpy (BUF, (const char *) CSTR );

 

Method 2 -----------------------------------------
For a Chinese System

Directly use (const char *) Str. local8bit ()
For example
Printf ("% s", (const char *) Str. local8bit ());

STR is a qstring

Method 3 -----------------------------------------
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, it can be converted from fromlocal8bit ()
STD: string functions with c_str () are converted to char *

Qstring has toascii () and cannot be remembered

You can check it out.

My carelessness caused a big mistake. I checked the QT document again. Originally, QT could generate a qstring directly from STD: wstring and use qstring: fromstdwstring (const STD :: wstring. I tried to use the qstring constructed by char * returned by c_str () of STD: String. instead of saving the original Chinese information, I used STD :: qstring constructed by wstring can use qdebug () to output the original Chinese information.
Conversion between GB encoding and utf8 Encoding
Add this sentence after the main function app:

Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("gb18030 "));

 

The conversion method from utf8 encoding to GB encoding is as follows:

Qstring utf8_to_gb (qstring strtext)
{
Return qstring: fromutf8 (strtext. tolocal8bit (). Data ());
}

 

From GB to utf8, we often use:

 

 


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.