首页 > 其他 > 详细

QT界面开发-GBK字符编码转换(中文乱码问题)

时间:2020-01-16 12:48:28      阅读:369      评论:0      收藏:0      [点我收藏+]

转载自 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

QT界面开发-GBK字符编码转换(中文乱码问题)

原文:https://www.cnblogs.com/nxopen2018/p/12200464.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!