Solve the problem of Chinese character string Transmission Encoding between QT program and windows program in Linux, and convert utf8 to GBK

Source: Internet
Author: User

Generally, UTF-8 encoding is used by default in Linux, So we write Qt in Linux.ProgramThe following two sentences are generally added:

 
Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("UTF-8"); qtextcodec: setcodecforcstrings (qtextcodec: codecforname ("UTF-8 "));

This sentence may also be added:

 
Qtextcodec: setcodecfortr (qtextcodec: codecforname ("UTF-8 "));

For more information, see.

After the above settings, it is okay to display basic Chinese in Linux. However, when we need to transmit strings with programs in windows, we can directly transmit the qstring, in Windows, Chinese characters are garbled, because Windows uses GBK encoding by default. Therefore, we need to convert the qstring encoding before sending:

Qtextcodec * gbkcodec = qtextcodec: codecforname ("GBK"); qtextcodec: setcodecforlocale (gbkcodec); smessage = gbkcodec-> tounicode (smessage. tolocal8bit (); // smessage is the string to be sent

Of course, we want the program in Linux to be displayed normally in the future. After sending the program, we need to make up the following sentence:

 
Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("UTF-8 "));

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.