Convert qstring to char *

Source: Internet
Author: User

In QT, qstring is equivalent to STD: String in C ++, or C style string in C. However, qstring is related to encoding. It is quite troublesome to send a qstring at the lower layer. In particular, when the other side is not using QT, it must first be converted to a char array.

To convert qstring to a char array, convert qstring to qbytearray first, and then obtain the char * pointer from qbytearray to obtain the char data. Note the encoding of qstring.

If it is UTF-8 encoded, the size () of qstring STR ("Chinese") is 3. The method for converting to char * is:

Str. toutf8 (). Data ()

In this case, the char * length is not equal to the length returned by the str. Size () function. If your code is Latin-1

Str. tolatin1 (). Data ()

Encoding is determined based on your own settings. If qtextcodec: setcodecforlocale (qtextcodec * C) is called, its own parameters shall prevail. If character encoding is not set in the code, it is related to the code file encoding. The file encoding of QT creator is usually utf8 in Linux and GBK in windows.

Convert qstring to char *

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.