转载自 https://chuanke.baidu.com/v4509752-209060-1284466.html
- GBK.h
- #ifndef _QT_GBK_H
- #define _QT_GBK_H
- #include <QString>
- #include <QTextCodec>
- #include <string>
- using std::string;
- class GBK
- {
- public:
- // QString(Unicode) -> std::string (GBK)
- static string FromUnicode(const QString& qstr)
- {
- QTextCodec* pCodec = QTextCodec::codecForName("gb2312");
- if(!pCodec) return "";
- QByteArray arr = pCodec->fromUnicode(qstr);
- string cstr = arr.data();
- return cstr;
- }
- // std::string (GBK) -> QString(Unicode)
- static QString ToUnicode(const string& cstr)
- {
- QTextCodec* pCodec = QTextCodec::codecForName("gb2312");
- if(!pCodec) return "";
- QString qstr = pCodec->toUnicode(cstr.c_str(), cstr.length());
- return qstr;
- }
- };
- #endif
来源: http://www.bubuko.com/infodetail-3383009.html