转载自 https://chuanke.baidu.com/v4509752-209060-1284466.html

GBK.h

 1 #ifndef _QT_GBK_H
 2 #define _QT_GBK_H
 3 
 4 
 5 #include <QString>
 6 #include <QTextCodec>
 7 #include <string>
 8 using std::string;
 9 
10 class GBK
11 {
12 public:
13     // QString(Unicode) -> std::string (GBK)
14     static string FromUnicode(const QString& qstr)
15     {
16         QTextCodec* pCodec = QTextCodec::codecForName("gb2312");
17         if(!pCodec) return "";    
18 
19         QByteArray arr = pCodec->fromUnicode(qstr);
20         string cstr = arr.data();
21         return cstr;
22     }
23 
24     // std::string (GBK) -> QString(Unicode)
25     static QString ToUnicode(const string& cstr)
26     {
27         QTextCodec* pCodec = QTextCodec::codecForName("gb2312");
28         if(!pCodec) return "";
29 
30         QString qstr = pCodec->toUnicode(cstr.c_str(), cstr.length());
31         return qstr;
32     }
33 
34 };
35 
36 
37 #endif

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-17
  • 2021-09-30
  • 2021-11-27
  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
相关资源
相似解决方案