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 *