Qt pure C programming printf output Chinese garbled solution

Google

Qt printf Chinese garbled characters        

foreword

1. This problem is caused by the inconsistency between QT's own text encoding settings and the text encoding settings when CMD displays.

2. The default text encoding of QtCreate is UTF-8, while the default encoding of the CMD console is GBK.

knowledge points

The two common encodings of Qt are: UTF-8 and GBK
★UTF-8: Unicode  TransformationFormat-8bit, which allows BOM, but usually does not contain BOM. It is a multi-byte encoding used to solve international characters. It uses 8 bits (one byte) for English and 24 bits (three bytes) for Chinese. UTF-8 contains characters that all countries in the world need to use. It is an international encoding and has strong versatility. UTF-8 encoded text can be displayed on browsers supporting UTF8 character sets in various countries. If it is encoded in UTF8, Chinese can also be displayed on the English IE of foreigners, and they do not need to download the Chinese language support package of IE.
★GBK is a standard compatible with GB2312 after expansion on the basis of national standard GB2312. GBK's text encoding is represented by double-bytes, that is, both Chinese and English characters are represented by double-bytes. In order to distinguish Chinese, the highest bit is set to 1. GBK contains all Chinese characters, is a national code, and is less versatile than UTF8, but UTF8 occupies a larger database than GBD. GBK is an extension of GB2312. In addition to being compatible with GB2312, it can also display traditional Chinese and Japanese pseudonyms.
★GBK, GB2312, etc. and UTF8 must be converted to each other through Unicode encoding:
GBK, GB2312--Unicode--UTF8
UTF8--Unicode--GBK, GB2312
★Under the simplified Chinese windows system, ANSI encoding represents GBK/GB2312 encoding, and ANSI usually uses 2 bytes in the range of 0x80~0xFF to represent a Chinese character. The characters between 0x00~0x7F are still 1 byte representing 1 character. For Unicode (UTF-16) encoding, all characters are represented by 2 bytes.

Solution

1. Use NotePad to change xx.c / xx.h to GBK

2. Qt settings are as follows

 test

int main()
{//SDK 入口点
    printf("Enter SDK's main()!\n");
    printf("这是一个中文打印测试\n");
    printf("the end!\n");
    getchar();

    user_main();
    return 0;
}

 

Guess you like

Origin blog.csdn.net/u012915636/article/details/124690046