Conversion of qstring and char *

Source: Internet
Author: User

During QT development, I often do not hesitate to convert the qstring to the char * type through toascii (). Data (), So I wasted more time searching for bugs.
Case:
In a project, you need to pass a long string, but use toascii (). the char * pointer obtained by data () conversion. During the running process, the pointer is often changed to garbled characters at a certain position, so the string is truncated, which leaves me confused several times.
Later, Baidu changed toascii (). Data () to tolatin1 (). Data (). The problem was solved.

In Main. CPP, the following Chinese encoding is often changed:

#include <QTextCodec>QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());QTextCodec::setCodecForTr(QTextCodec::codecForLocale());

Therefore, if the qstring contains Chinese characters, use toascii () conversion. If only English characters exist, use tolatin1.

The QT document says:
Qbytearray qstring: toascii () const

Returns an 8-bit representation of the string as a qbytearray.

If a codec has been set using qtextcodec: setcodecforcstrings (), it is used to convert Unicode to 8-bit char; otherwise this function does the same as tolatin1 ().

Note that, despite the name, this function does not necessarily return an US-ASCII (ANSI X3.4-1986) string and its result may not be US-ASCII compatible.

If the setcodecforcstrings function is used for encoding, toascii converts Unicode to an 8-bit char type. Otherwise, it is the same as tolatin1.

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.