QString Chinese encoding conversion

 
Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https: //blog.csdn.net/xxm524/article/details/74937308

QT encountered a lot of character encoding the development of the pit, believe there will be Chinese garbled summary of what here.
 QString itself is coded is unicode
in windows local8Bit is GBK
 source i.e. .cpp file is encoded, different compilers have default encoding, such as:
the cl Microsoft VS using GBK
UTF the Mingw in g ++ without the BOM . 8
G ++ under Linux using UTF-8 encoding these saved with BOM 3
(So, Chinese hard code, used to find out the code in the encoding format of the compiler itself)
 
QString GBK2UTF8(const QString &str)
{
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
    return utf8->toUnicode(str.toUtf8());
}
 
QString UTF82GBK(const QString &str)
{
    QTextCodec *gbk = QTextCodec::codecForName("GB18030");
    return gbk->toUnicode(str.toLocal8Bit());
}
 
std::string GBK2UTF8(std::string &str)
{
    QString temp = QString::fromLocal8Bit(str.c_str());
    std::string ret = temp.toUtf8().data();
    Return the right;
}
 
std::string UTF82GBK(std::string &str)
{
    QString temp = QString::fromUtf8(str.c_str());
    std::string ret = temp.toLocal8Bit().data();
    Return the right;
}

 

Guess you like

Origin www.cnblogs.com/lvdongjie/p/11821318.html